<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no"><title>JavaScript基础 | XC's Blog</title><meta name="keywords" content="前端笔记"><meta name="author" content="XC's Blog"><meta name="copyright" content="XC's Blog"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="「学习笔记」JavaScript基础编程基础「计算机语言」分为机器语言，汇编语言，高级语言。计算机内部最终执行的都是机器语言，由0和1这样的二进制数构成。 「数据存储单位」8bit(比特) &#x3D; 1B(Byte)字节  千字节1KB &#x3D; 1024B 「翻译器」高级语言编写的程序不能被计算机识别，需要经过转换，将源代码程序翻译成机器语言才能运行。浏览器里面的js解释器就是这样的一个"><meta property="og:type" content="article"><meta property="og:title" content="JavaScript基础"><meta property="og:url" content="https://www.webxc.ml/post/2847555892/index.html"><meta property="og:site_name" content="XC&#39;s Blog"><meta property="og:description" content="「学习笔记」JavaScript基础编程基础「计算机语言」分为机器语言，汇编语言，高级语言。计算机内部最终执行的都是机器语言，由0和1这样的二进制数构成。 「数据存储单位」8bit(比特) &#x3D; 1B(Byte)字节  千字节1KB &#x3D; 1024B 「翻译器」高级语言编写的程序不能被计算机识别，需要经过转换，将源代码程序翻译成机器语言才能运行。浏览器里面的js解释器就是这样的一个"><meta property="og:locale" content="zh_CN"><meta property="og:image" content="https://npm.elemecdn.com/webxc@1.0.0/cover/js1.jpg"><meta property="article:published_time" content="2022-01-05T18:00:00.000Z"><meta property="article:modified_time" content="2022-10-23T15:52:01.938Z"><meta property="article:author" content="XC&#39;s Blog"><meta property="article:tag" content="前端笔记"><meta name="twitter:card" content="summary"><meta name="twitter:image" content="https://npm.elemecdn.com/webxc@1.0.0/cover/js1.jpg"><link rel="shortcut icon" href="https://npm.elemecdn.com/webxc@1.0.0/logo/logo.jpg"><link rel="canonical" href="https://www.webxc.ml/post/2847555892/"><link rel="preconnect" href="//cdn.jsdelivr.net"><link rel="preconnect" href="//busuanzi.ibruce.info"><link rel="stylesheet" href="/css/index.css"><link rel="stylesheet" href="https://cdn.bootcdn.net/ajax/libs/font-awesome/6.2.0/css/all.min.css" media="print" onload='this.media="all"'><link rel="stylesheet" href="https://npm.elemecdn.com/node-snackbar/dist/snackbar.min.css" media="print" onload='this.media="all"'><link rel="stylesheet" href="https://npm.elemecdn.com/@fancyapps/ui/dist/fancybox.css" media="print" onload='this.media="all"'><script>const GLOBAL_CONFIG={root:"/",algolia:void 0,localSearch:{path:"/search.xml",preload:!1,languages:{hits_empty:"找不到您查询的内容：${query}"}},translate:void 0,noticeOutdate:void 0,highlight:{plugin:"highlighjs",highlightCopy:!0,highlightLang:!0,highlightHeightLimit:!1},copy:{success:"吾辈宣布,你的剪切板已被占领",error:"呀~复制失败,请在尝试下",noSupport:"对不起,你的浏览器不支持"},relativeDate:{homepage:!1,post:!1},runtime:"",date_suffix:{just:"刚刚",min:"分钟前",hour:"小时前",day:"天前",month:"个月前"},copyright:{limitCount:60,languages:{author:"作者: XC's Blog",link:"链接: ",source:"来源: XC's Blog",info:"著作权归作者所有。商业转载请联系作者获得授权，非商业转载请注明出处。"}},lightbox:"fancybox",Snackbar:{chs_to_cht:"您已切换为繁体",cht_to_chs:"您已切换为简体",day_to_night:"您已切换为深色主题",night_to_day:"您已切换为浅色主题",bgLight:"#425AEF",bgDark:"#121212",position:"top-center"},source:{justifiedGallery:{js:"https://npm.elemecdn.com/flickr-justified-gallery/dist/fjGallery.min.js",css:"https://npm.elemecdn.com/flickr-justified-gallery/dist/fjGallery.css"}},isPhotoFigcaption:!1,islazyload:!0,isAnchor:!0}</script><script id="config-diff">var GLOBAL_CONFIG_SITE={title:"JavaScript基础",isPost:!0,isHome:!1,isHighlightShrink:!1,isToc:!0,postUpdate:"2022-10-23 15:52:01"}</script><noscript><style>#nav{opacity:1}.justified-gallery img{opacity:1}#post-meta time,#recent-posts time{display:inline!important}</style></noscript><script>(e=>{e.saveToLocal={set:function(e,t,o){if(0===o)return;const n=864e5*o,a={value:t,expiry:(new Date).getTime()+n};localStorage.setItem(e,JSON.stringify(a))},get:function(e){const t=localStorage.getItem(e);if(!t)return;const o=JSON.parse(t);if(!((new Date).getTime()>o.expiry))return o.value;localStorage.removeItem(e)}},e.getScript=e=>new Promise(((t,o)=>{const n=document.createElement("script");n.src=e,n.async=!0,n.onerror=o,n.onload=n.onreadystatechange=function(){const e=this.readyState;e&&"loaded"!==e&&"complete"!==e||(n.onload=n.onreadystatechange=null,t())},document.head.appendChild(n)})),e.activateDarkMode=function(){document.documentElement.setAttribute("data-theme","dark"),null!==document.querySelector('meta[name="theme-color"]')&&document.querySelector('meta[name="theme-color"]').setAttribute("content","#0d0d0d")},e.activateLightMode=function(){document.documentElement.setAttribute("data-theme","light"),null!==document.querySelector('meta[name="theme-color"]')&&document.querySelector('meta[name="theme-color"]').setAttribute("content","#ffffff")};const t=saveToLocal.get("theme");"dark"===t?activateDarkMode():"light"===t&&activateLightMode();const o=saveToLocal.get("aside-status");void 0!==o&&("hide"===o?document.documentElement.classList.add("hide-aside"):document.documentElement.classList.remove("hide-aside"));/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)&&document.documentElement.classList.add("apple")})(window)</script><link rel="stylesheet" href="https://npm.elemecdn.com/webxc@1.0.11/css/styles.css"><link rel="stylesheet" href="https://npm.elemecdn.com/webxc@1.0.11/css/font.css"><link rel="stylesheet" href="https://npm.elemecdn.com/webxc@1.0.11/css/styless.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/Zfour/Butterfly-double-row-display@1.00/cardlistpost.min.css"><style>#recent-posts>.recent-post-item>.recent-post-info>.article-meta-wrap>.tags:before{content:"\A";white-space:pre}#recent-posts>.recent-post-item>.recent-post-info>.article-meta-wrap>.tags>.article-meta__separator{display:none}</style><meta name="generator" content="Hexo 6.3.0"></head><body><div id="web_bg"></div><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src="https://npm.elemecdn.com/webxc@1.0.0/logo/logo.jpg" onerror='onerror=null,src="https://npm.elemecdn.com/webxc@1.0.2/banner/404.gif"' alt="avatar"></div><div class="sidebar-site-data site-data is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">53</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">18</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">18</div></a></div><hr><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i> <span>首页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i> <span>时间轴</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i> <span>标签</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i> <span>分类</span></a></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i> <span>友联</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i> <span>关于</span></a></div><div class="menus_item"><a class="site-page" href="https://www.webxc.ml/music/"><i class="fa-fw fas fa-music"></i> <span>音乐解锁</span></a></div><div class="menus_item"><a class="site-page" target="_blank" rel="noopener" href="https://site.51git.cn/render/gonav/4.html"><i class="fa-fw fa-brands fa-internet-explorer"></i> <span>网址导航</span></a></div><div class="menus_item"><a class="site-page group hide" href="javascript:void(0);"><i class="fa-fw fas fa-list"></i> <span>推荐</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" target="_blank" rel="noopener" href="http://www.inode.club/"><i class="fa-fw fa-sharp fa-solid fa-square-rss"></i> <span>程序员指南</span></a></li><li><a class="site-page child" target="_blank" rel="noopener" href="http://ife.baidu.com/"><i class="fa-fw fa-brands fa-vuejs"></i> <span>前端技术学园</span></a></li><li><a class="site-page child" target="_blank" rel="noopener" href="https://www.yuque.com/tademanong/ag7q4n"><i class="fa-fw fa-solid fa-users"></i> <span>前端技术圈</span></a></li><li><a class="site-page child" target="_blank" rel="noopener" href="https://www.code-nav.cn/"><i class="fa-fw fa-solid fa-shop"></i> <span>编程导航</span></a></li><li><a class="site-page child" target="_blank" rel="noopener" href="https://q.shanyue.tech/"><i class="fa-fw fa-brands fa-square-js"></i> <span>每日一题</span></a></li><li><a class="site-page child" target="_blank" rel="noopener" href="https://fanyouf.gitee.io/u1word/#/h5"><i class="fa-fw fa-solid fa-headphones"></i> <span>前端必会单词</span></a></li></ul></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image:url('https://npm.elemecdn.com/webxc@1.0.0/cover/js1.jpg')"><nav id="nav"><span id="blog_name"><a id="site-name" href="/">XC's Blog</a></span><div id="menus"><div id="search-button"><a class="site-page social-icon search"><i class="fas fa-search fa-fw"></i> <span>搜索</span></a></div><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i> <span>首页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i> <span>时间轴</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i> <span>标签</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i> <span>分类</span></a></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i> <span>友联</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i> <span>关于</span></a></div><div class="menus_item"><a class="site-page" href="https://www.webxc.ml/music/"><i class="fa-fw fas fa-music"></i> <span>音乐解锁</span></a></div><div class="menus_item"><a class="site-page" target="_blank" rel="noopener" href="https://site.51git.cn/render/gonav/4.html"><i class="fa-fw fa-brands fa-internet-explorer"></i> <span>网址导航</span></a></div><div class="menus_item"><a class="site-page group hide" href="javascript:void(0);"><i class="fa-fw fas fa-list"></i> <span>推荐</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" target="_blank" rel="noopener" href="http://www.inode.club/"><i class="fa-fw fa-sharp fa-solid fa-square-rss"></i> <span>程序员指南</span></a></li><li><a class="site-page child" target="_blank" rel="noopener" href="http://ife.baidu.com/"><i class="fa-fw fa-brands fa-vuejs"></i> <span>前端技术学园</span></a></li><li><a class="site-page child" target="_blank" rel="noopener" href="https://www.yuque.com/tademanong/ag7q4n"><i class="fa-fw fa-solid fa-users"></i> <span>前端技术圈</span></a></li><li><a class="site-page child" target="_blank" rel="noopener" href="https://www.code-nav.cn/"><i class="fa-fw fa-solid fa-shop"></i> <span>编程导航</span></a></li><li><a class="site-page child" target="_blank" rel="noopener" href="https://q.shanyue.tech/"><i class="fa-fw fa-brands fa-square-js"></i> <span>每日一题</span></a></li><li><a class="site-page child" target="_blank" rel="noopener" href="https://fanyouf.gitee.io/u1word/#/h5"><i class="fa-fw fa-solid fa-headphones"></i> <span>前端必会单词</span></a></li></ul></div></div><div id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">JavaScript基础</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2022-01-05T18:00:00.000Z" title="发表于 2022-01-05 18:00:00">2022-01-05</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2022-10-23T15:52:01.938Z" title="更新于 2022-10-23 15:52:01">2022-10-23</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/%E5%89%8D%E7%AB%AF%E7%AC%94%E8%AE%B0/">前端笔记</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span data-flag-title="JavaScript基础"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="twikoo_visitors"><i class="fa-solid fa-spinner fa-spin"></i></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><h3 id="「学习笔记」JavaScript基础"><a href="#「学习笔记」JavaScript基础" class="headerlink" title="「学习笔记」JavaScript基础"></a><strong>「学习笔记」JavaScript基础</strong></h3><h4 id="编程基础"><a href="#编程基础" class="headerlink" title="编程基础"></a>编程基础</h4><p><strong>「计算机语言」</strong>分为机器语言，汇编语言，高级语言。计算机内部最终执行的都是机器语言，由<code>0</code>和<code>1</code>这样的二进制数构成。</p><p><strong>「数据存储单位」</strong>8bit(比特) &#x3D; 1B(Byte)字节 千字节1KB &#x3D; 1024B</p><p><strong>「翻译器」</strong>高级语言编写的程序不能被计算机识别，需要经过转换，将源代码程序翻译成<code>机器语言</code>才能运行。浏览器里面的js解释器就是这样的一个翻译器。</p><p><strong>「程序运行」</strong></p><ul><li>打开某个程序时，先从硬盘中把程序的代码加载到内存中</li><li>CPU执行内存中的代码</li><li>注意：之所以要内存的一个重要原因，是因为 cpu运行太快了，如果只从硬盘中读数据，会浪费cpu性能，所以，才使用存取速度更快的内存来保存运行时的数据。（内存是电，硬盘是机械）</li></ul><h3 id="初识JavaScript"><a href="#初识JavaScript" class="headerlink" title="初识JavaScript"></a>初识JavaScript</h3><p><strong>「创始人」</strong>布兰登·艾奇(Brendan Eich),起初命名为<code>LiveScript</code>后来与Sun公司合作改名为<code>JavaScript</code>。</p><p><strong>「JavaScript」</strong>运行在客户端的脚本语言，不需要编译，由js解释器(js引擎)逐行解释执行。Node.js也可以用于服务器端编程。</p><p><strong>「JavaScript组成」</strong>ECMAScript(JavaScript语法)、DOM(文档对象模型)、BOM(浏览器对象模型)</p><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="title class_">JavaScript</span>的作用</span><br></pre></td></tr></table></figure><ul><li>表单动态校验(密码强度检测)</li><li>网页特效</li><li>服务端开发(Node.js)</li><li>桌面程序(Electron)、App(Cordova)、控制硬件-物联网(Ruff)、游戏开发(cocos2d-js)</li></ul><p><strong>「JavaScript书写位置」</strong></p><blockquote><p>JS有3种书写位置，分别为行内、内嵌和外部。</p></blockquote><ol><li>行内式</li></ol><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">&lt;input type=<span class="string">&quot;button&quot;</span> value=<span class="string">&quot;点我试试&quot;</span> </span><br><span class="line">onclick=<span class="string">&quot;alert(&#x27;Hello World&#x27;)&quot;</span> /&gt;</span><br></pre></td></tr></table></figure><ol><li>内嵌式</li></ol><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">&lt;script&gt;</span><br><span class="line">    <span class="title function_">alert</span>(<span class="string">&#x27;Hello  World~!&#x27;</span>);</span><br><span class="line">&lt;/script&gt;</span><br></pre></td></tr></table></figure><ol><li>外部式</li></ol><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">引用外部js文件</span><br><span class="line">&lt;script src = <span class="string">&quot;my.js&quot;</span>&gt;&lt;/script&gt;</span><br></pre></td></tr></table></figure><p><strong>「注释」</strong></p><ol><li>单行注释</li></ol><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">// 我是单行注释 (快捷键 ctrl + / )</span><br></pre></td></tr></table></figure><ol><li>多行注释</li></ol><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">/*</span><br><span class="line">  获取用户年龄和姓名</span><br><span class="line">  并通过提示框显示出来</span><br><span class="line">  点击vscode左下角管理-键盘快捷方式-切换块注释</span><br><span class="line">  (默认快捷键 alt + shift + a) 修改为 (ctrl + shift + /)</span><br><span class="line">*/</span><br></pre></td></tr></table></figure><h2 id="变量"><a href="#变量" class="headerlink" title="变量"></a>变量</h2><h3 id="变量的概念"><a href="#变量的概念" class="headerlink" title="变量的概念"></a>变量的概念</h3><p><strong>「变量」</strong>是程序在内存中申请的一块用于存放数据的空间。变量是用于存放数据的容器，可以通过变量名获取数据，甚至修改数据。<img src="" data-lazy-src="" alt="图片"></p><h3 id="变量的使用"><a href="#变量的使用" class="headerlink" title="变量的使用"></a>变量的使用</h3><p><strong>「1. 声明变量」</strong></p><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 1.声明变量</span></span><br><span class="line"><span class="keyword">var</span> num; <span class="comment">// 声明一个 名称为 num 的变量</span></span><br></pre></td></tr></table></figure><p>var是一个JS关键字，用来声明变量(variable变量的意思)。num是我们定义的变量名，可以通过变量名来访问内存中分配的空间。</p><p><strong>「2. 赋值」</strong></p><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">num = <span class="number">10</span>;<span class="comment">//给 num 这个变量赋值为 10</span></span><br></pre></td></tr></table></figure><p><strong>「3. 变量的初始化」</strong></p><p>声明一个变量并赋值， 我们称之为变量的初始化。</p><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">var num = 10;//声明变量并赋值为10 </span><br></pre></td></tr></table></figure><p><strong>「4. 变量语法扩展」</strong></p><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 1.一个变量被重新赋值后，它原有的值会被覆盖掉，变量值以最后一次赋的值为准。</span></span><br><span class="line"><span class="keyword">var</span> num = <span class="number">10</span>;</span><br><span class="line">num = <span class="number">11</span>;</span><br><span class="line"><span class="comment">// 2.同时声明多个变量(只需要写一个var,多个变量名之间用英文逗号隔开)</span></span><br><span class="line"><span class="keyword">var</span> num = <span class="number">10</span>, age = <span class="number">15</span>, name = <span class="string">&#x27;fan&#x27;</span>;</span><br></pre></td></tr></table></figure><p>声明变量特殊情况</p><table><thead><tr><th align="center">情况</th><th align="center">说明</th><th align="center">结果</th></tr></thead><tbody><tr><td align="center">var age ; console.log (age);</td><td align="center">只声明 不赋值</td><td align="center">undefined</td></tr><tr><td align="center">console.log(age)</td><td align="center">不声明 不赋值 直接使用</td><td align="center">报错</td></tr><tr><td align="center">age &#x3D; 10; console.log (age);</td><td align="center">不声明 只赋值</td><td align="center">10</td></tr></tbody></table><p><strong>「5. 变量命名规范」</strong><img src="" data-lazy-src="https://cdn1.tianli0.top/gh/web-xc/iMG/posts/xuexijs%20(10).png" alt="图片"></p><h2 id="数据类型"><a href="#数据类型" class="headerlink" title="数据类型"></a>数据类型</h2><p>分为两类：简单数据类型(Number,String,Boolean,Undefined,Null)和复杂数据类型(object)。</p><table><thead><tr><th align="left">简单数据类型</th><th align="left">说明</th><th align="left">默认值</th></tr></thead><tbody><tr><td align="left">Number</td><td align="left">数字型,包含整型值和浮点型值</td><td align="left">0</td></tr><tr><td align="left">String</td><td align="left">字符串型</td><td align="left">“”</td></tr><tr><td align="left">Boolean</td><td align="left">布尔值型</td><td align="left">false</td></tr><tr><td align="left">Undefined</td><td align="left">var a;声明了变量a但是没有赋值，此时a &#x3D; undefined</td><td align="left">undefined</td></tr><tr><td align="left">Null</td><td align="left">var a &#x3D; null;声明了变量a为空值</td><td align="left">null</td></tr></tbody></table><h3 id="Number数字型"><a href="#Number数字型" class="headerlink" title="Number数字型"></a>Number数字型</h3><p><strong>「数字型进制」</strong></p><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">// 1.在JS中八进制前面加0，十六进制前面加 0x</span><br><span class="line">var num1 = 07;   // 对应十进制的7</span><br><span class="line">// 2.十六进制数字序列范围：0~9以及A~F</span><br><span class="line"> var num = 0xA;   </span><br></pre></td></tr></table></figure><p><strong>「数字型范围」</strong>JavaScript中的数值有最大值和最小值</p><ul><li>最大值:<code>Number.MAX_VALUE</code>，值为：1.7976931348623157e+308</li><li>最小值:<code>Number.MIN_VALUE</code>，值为：5e-32</li><li>特殊值：<code>Infinity</code> 无穷大 <code>-Infinity</code> 无穷小 <code>NaN</code> 代表一个非数字</li><li>isNaN():用来判断一个变量是否为非数字的类型。非数字型为true,数字型为false。</li></ul><h3 id="String字符串型"><a href="#String字符串型" class="headerlink" title="String字符串型"></a>String字符串型</h3><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">// 1.字符串型可以是引号中的任意文本，语法为 单引号 和 双引号</span><br><span class="line">var msg = &#x27;我的名字叫&#x27;;</span><br><span class="line">var name = &quot;fan&quot;;</span><br></pre></td></tr></table></figure><p><strong>「1. 字符串转义符」</strong>都是以 \ 开头，详细如下👇👇</p><table><thead><tr><th align="left">转义符</th><th align="left">说明</th></tr></thead><tbody><tr><td align="left">\n</td><td align="left">换行符，n是 newline 的意思</td></tr><tr><td align="left">\</td><td align="left">斜杆 \</td></tr><tr><td align="left">&#39;</td><td align="left">单引号 ‘</td></tr><tr><td align="left">&quot;</td><td align="left">双引号 “</td></tr><tr><td align="left">\t</td><td align="left">tab 缩进</td></tr><tr><td align="left">\b</td><td align="left">空格,b是 blank 的意思</td></tr></tbody></table><p><strong>「2. 字符串长度」</strong><br>字符串是由若干字符组成的，这些字符的数量就是字符串的长度。</p><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">// 1.字符串型可以是引号中的任意文本，语法为 单引号 和 双引号</span><br><span class="line">var msg = &#x27;我是帅气的饭老板&#x27;;</span><br><span class="line">console.log(msg.length); // 显示 8</span><br></pre></td></tr></table></figure><p><strong>「3. 字符串拼接」</strong><br>多个字符串之间可以使用 + 进行拼接，其拼接方式为 字符串 + 任何类型 &#x3D; 拼接之后的新字符串。<br>拼接前会把与字符串相加的任何类型转成字符串，再拼接成一个新的字符串</p><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">//1.1 字符串 &quot;相加&quot;</span><br><span class="line">alert(&#x27;hello&#x27; + &#x27; &#x27; + &#x27;world&#x27;); // hello world</span><br><span class="line">//1.2 数值字符串 &quot;相加&quot;</span><br><span class="line">alert(&#x27;100&#x27; + &#x27;100&#x27;); // 100100</span><br><span class="line">//1.3 数值字符串 + 数值</span><br><span class="line">alert(&#x27;11&#x27; + 12);     // 1112 +号口诀：数值相加，字符相连</span><br><span class="line">// 1.4 字符串拼接加强</span><br><span class="line">var age = 18;</span><br><span class="line">alert(&quot;饭老板今年&quot; + age +&quot;岁了&quot;);</span><br></pre></td></tr></table></figure><h3 id="布尔型Boolean"><a href="#布尔型Boolean" class="headerlink" title="布尔型Boolean"></a>布尔型Boolean</h3><p>布尔类型有两个值：true 和 false ，其中 true 表示真（对），而 false 表示假（错）。<br>布尔型和数字型相加的时候， true 的值为 1 ，false 的值为 0。</p><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">console.log(true + 1) // 2</span><br><span class="line">console.log(false + 1) // 1</span><br></pre></td></tr></table></figure><h3 id="Undefined-和-Null"><a href="#Undefined-和-Null" class="headerlink" title="Undefined 和 Null"></a>Undefined 和 Null</h3><p>一个变量声明后没有赋值会有一个默认值undefined(如果相连或者相加时，注意结果😊)</p><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">var variable;</span><br><span class="line">console.log(variable); // undefined</span><br><span class="line">console.log(&quot;你好&quot; + variable); // 你好undefined</span><br><span class="line">console.log(11 + variable); // NaN</span><br><span class="line">console.log(true + variable);// NaN</span><br></pre></td></tr></table></figure><p>一个变量声明并赋值null,里面存的值为空</p><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">var var2 = null;</span><br><span class="line">console.log(var2); // null</span><br><span class="line">console.log(&quot;你好&quot; + var2); // 你好null</span><br><span class="line">console.log(11 + var2); // 11</span><br><span class="line">console.log(true + var2);// 1</span><br></pre></td></tr></table></figure><h3 id="获取变量类型及转换"><a href="#获取变量类型及转换" class="headerlink" title="获取变量类型及转换"></a>获取变量类型及转换</h3><ul><li>检测变量的数据类型 <code>typeof</code></li></ul><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">var num = 10;</span><br><span class="line">console.log(typeof num)//结果为 number</span><br></pre></td></tr></table></figure><ul><li><p><code>字面量</code>:是源代码中一个固定值的表示法，就是字面量如何去表达这个值。通过数据的格式特征可以判断数据的类型</p></li><li><ul><li>有数字字面量:8,9,10</li><li>字符串字面量:’饭老板’，”前端开发”</li><li>布尔字面量:true,false</li></ul></li></ul><p><strong>「数据类型转换」</strong></p><ul><li>转换为字符串</li></ul><table><thead><tr><th align="left">方式</th><th align="left">说明</th><th align="left">案例</th></tr></thead><tbody><tr><td align="left">toString()</td><td align="left">转成字符串</td><td align="left">var num&#x3D;1; alert(num.toString())</td></tr><tr><td align="left">String()</td><td align="left">强制转换</td><td align="left">var num&#x3D;1; alert(String(num))</td></tr><tr><td align="left"><strong>加号拼接字符串</strong></td><td align="left">和字符串拼接的结果都是字符串</td><td align="left">var num&#x3D;1; alert(num+’我是字符串’)</td></tr></tbody></table><ul><li>转换为数字型</li></ul><table><thead><tr><th align="left">方式</th><th align="left">说明</th><th align="left">案例</th></tr></thead><tbody><tr><td align="left">parseInt(String)函数</td><td align="left">将string类型转成整数型</td><td align="left">parseInt(‘11’)</td></tr><tr><td align="left">parseFloat(String)函数</td><td align="left">将string类型转成浮点型</td><td align="left">parseFloat(‘11.2’)</td></tr><tr><td align="left">Number() 强制转换函数</td><td align="left">将string类型强制转换为数值型</td><td align="left">Number(‘12’)</td></tr><tr><td align="left"><strong>js隐式转换 ( - * &#x2F;)</strong></td><td align="left">利用算数运算隐式转换为数值型</td><td align="left">‘12’ - 0</td></tr></tbody></table><ul><li>转换为布尔型<br>代表空、否定的值会被转换为false，如’’、0、NaN、null、undefined 其余值都会被转换为true</li></ul><table><thead><tr><th align="left">方式</th><th align="left">说明</th><th align="left">案例</th></tr></thead><tbody><tr><td align="left">Boolean()</td><td align="left">其他类型转换为布尔值</td><td align="left">Boolean(‘true’)</td></tr></tbody></table><h3 id="关键字和保留字"><a href="#关键字和保留字" class="headerlink" title="关键字和保留字"></a>关键字和保留字</h3><p><strong>「标识符」</strong>指开发人员为变量、属性、函数、参数取得名字。标识符不能是关键字或保留字。</p><p><strong>「关键字」</strong>指 JS本身已经使用了的字，不能再用它们充当变量名、方法名</p><blockquote><p>包括：break、case、catch、continue、default、delete、do、else、finally、for、function、if、in、instanceof、new、return、switch、this、throw、try、typeof、var、void、while、with 等。</p></blockquote><p><strong>「保留字」</strong>实际上就是预留的“关键字”，意思是现在虽然还不是关键字，但是未来可能会成为关键字，同样不能使用它们当变量名或方法名。</p><blockquote><p>boolean、byte、char、class、const、debugger、double、enum、export、extends、fimal、float、goto、implements、import、int、interface、long、mative、package、private、protected、public、short、static、super、synchronized、throws、transient、volatile 等。</p><p>注意：如果将保留字用作变量名或函数名，那么除非将来的浏览器实现了该保留字，否则很可能收不到任何错误消息。当浏览器将其实现后，该单词将被看做关键字，如此将出现关键字错误。</p></blockquote><hr><h3 id="运算符与流程控制"><a href="#运算符与流程控制" class="headerlink" title="运算符与流程控制"></a>运算符与流程控制</h3><h4 id="运算符-操作符"><a href="#运算符-操作符" class="headerlink" title="运算符(操作符)"></a>运算符(操作符)</h4><p><strong>「运算符」</strong>是用于实现赋值、比较和执行算数运算等功能的符号。常用运算符分类如下👇</p><ul><li>算数运算符</li><li>递增和递减运算符</li><li>比较运算符</li><li>逻辑运算符</li><li>赋值运算符</li></ul><p><strong>「算数运算符」</strong></p><table><thead><tr><th align="left">运算符</th><th align="left">描述</th><th align="left">案例</th></tr></thead><tbody><tr><td align="left">+</td><td align="left">加</td><td align="left">10+20&#x3D;30</td></tr><tr><td align="left">-</td><td align="left">减</td><td align="left">10-20&#x3D;-10</td></tr><tr><td align="left">*</td><td align="left">乘</td><td align="left">10*20&#x3D;200</td></tr><tr><td align="left">&#x2F;</td><td align="left">除</td><td align="left">10&#x2F;20&#x3D;0.5</td></tr><tr><td align="left">%</td><td align="left">取余(取模)</td><td align="left">返回除法的余数9%2&#x3D;1</td></tr></tbody></table><ul><li><p>浮点数的精度问题</p><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">var result = 0.1 + 0.2;    // 结果不是 0.3，而是：0.30000000000000004</span><br><span class="line">console.log(0.07 * 100);   // 结果不是 7，  而是：7.000000000000001</span><br></pre></td></tr></table></figure></li><li><ul><li>浮点数值的最高精度是17位小数，但是在进行算数运算时其精确度远远不如整数,所以不要直接判断两个浮点数是否相等!</li></ul></li><li><p>表达式与返回值</p></li><li><ul><li>表达式：由数字、运算符和变量组成的式子。</li><li>返回值：每一个表达式经过相应的运算之后，会有一个最终结果，称为表达式的返回值</li></ul></li></ul><p><strong>「递增和递减运算符」</strong></p><p>递增和递减运算符必须配合变量使用。</p><ul><li>递增运算符</li></ul><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line">var  num = 10;</span><br><span class="line">alert(++num + 10);   // 21 使用口诀：先自加，后返回值</span><br><span class="line"></span><br><span class="line">var  num1 = 10;</span><br><span class="line">alert(10 + num1++);  // 20 使用口诀：先返回原值，后自加 </span><br><span class="line">var num = 1;</span><br><span class="line">var num2 = ++num + num++; //num = 2</span><br><span class="line">console.log(num2);//4</span><br><span class="line"></span><br><span class="line">var num = 1;</span><br><span class="line">var num1 = 1;</span><br><span class="line">var num2 = num++ + num1++; // 1 + 1</span><br><span class="line">console.log(num2);//2</span><br><span class="line"></span><br><span class="line">var num = 1;</span><br><span class="line">var num2 = num++ + num++;// 1 + 2 </span><br><span class="line">console.log(num2); // 3  </span><br><span class="line"></span><br></pre></td></tr></table></figure><p><strong>「比较运算符」</strong></p><table><thead><tr><th align="center">运算符</th><th align="left">描述</th><th align="left">案例</th><th align="left">结果</th></tr></thead><tbody><tr><td align="center">&lt;</td><td align="left">小于号</td><td align="left">1&lt;2</td><td align="left">true</td></tr><tr><td align="center">&gt;</td><td align="left">大于号</td><td align="left">1&gt;2</td><td align="left">false</td></tr><tr><td align="center">&gt;&#x3D;</td><td align="left">大于等于号(大于或者等于)</td><td align="left">2 &gt;&#x3D; 2</td><td align="left">true</td></tr><tr><td align="center">&lt;&#x3D;</td><td align="left">小于等于号(小于或者等于)</td><td align="left">3 &lt;&#x3D; 2</td><td align="left">false</td></tr><tr><td align="center">&#x3D;&#x3D;</td><td align="left">判等号(会转型)</td><td align="left">15 &#x3D;&#x3D; ‘15’</td><td align="left">true</td></tr><tr><td align="center">!&#x3D;</td><td align="left">不等号</td><td align="left">37 !&#x3D; 37</td><td align="left">false</td></tr><tr><td align="center">&#x3D;&#x3D;&#x3D; !&#x3D;&#x3D;&#x3D;</td><td align="left">全等 全不等(要求值和数据类型都一致)</td><td align="left">37 &#x3D;&#x3D;&#x3D; ‘37’</td><td align="left">false</td></tr></tbody></table><p><strong>「逻辑运算符」</strong><br>逻辑运算符是用来进行布尔值运算的运算符<br>短路运算:当有多个表达式（值）时,左边的表达式值可以确定结果时,就不再继续运算右边的表达式的值;</p><table><thead><tr><th align="center">运算符</th><th align="left">描述</th><th align="left">案例</th><th align="left">特点</th></tr></thead><tbody><tr><td align="center">&amp;&amp;</td><td align="left">“逻辑与”,简称”与” and</td><td align="left">true &amp;&amp; false</td><td align="left">两边都是 true才返回 true</td></tr><tr><td align="center">||</td><td align="left">“逻辑或”,简称”或” or</td><td align="left">true</td><td align="left">有真为真</td></tr><tr><td align="center">!</td><td align="left">“逻辑非”,简称”非” not</td><td align="left">!true</td><td align="left">取反</td></tr></tbody></table><p><strong>「赋值运算符」</strong></p><table><thead><tr><th align="center">运算符</th><th align="left">描述</th><th align="left">案例</th></tr></thead><tbody><tr><td align="center">&#x3D;</td><td align="left">直接赋值</td><td align="left">var userName &#x3D; ‘fan’</td></tr><tr><td align="center">+&#x3D; -&#x3D;</td><td align="left">加减一个数后再赋值</td><td align="left">var age&#x3D;5; age+&#x3D;5</td></tr><tr><td align="center">*&#x3D; &#x2F;&#x3D; %&#x3D;</td><td align="left">乘、除、取模后再赋值</td><td align="left">var age&#x3D;5; age*&#x3D;5</td></tr></tbody></table><p><strong>「运算符优先级」</strong></p><table><thead><tr><th align="center">优先级</th><th align="left">运算符</th><th align="left">顺序</th></tr></thead><tbody><tr><td align="center">1</td><td align="left">小括号</td><td align="left">()</td></tr><tr><td align="center">2</td><td align="left">一元运算符</td><td align="left">! ++ –</td></tr><tr><td align="center">3</td><td align="left">算数运算符</td><td align="left">先* &#x2F; % 后+ -</td></tr><tr><td align="center">4</td><td align="left">关系运算符</td><td align="left">&gt; &gt;&#x3D; &lt; &lt;&#x3D;</td></tr><tr><td align="center">5</td><td align="left">相等运算符</td><td align="left">&#x3D;&#x3D; !&#x3D; &#x3D;&#x3D;&#x3D; !&#x3D;&#x3D;</td></tr><tr><td align="center">6</td><td align="left">逻辑运算符</td><td align="left">先&amp;&amp; 后||</td></tr><tr><td align="center">7</td><td align="left">赋值运算符</td><td align="left">&#x3D;</td></tr><tr><td align="center">8</td><td align="left">逗号运算符</td><td align="left">,</td></tr></tbody></table><h3 id="流程控制"><a href="#流程控制" class="headerlink" title="流程控制"></a>流程控制</h3><p><strong>「流程控制」</strong>在一个程序执行的过程中，各条代码的执行顺序对程序的结果是有直接影响的。很多时候我们要通过控制代码的执行顺序来实现我们要完成的功能。流程控制主要有三种结构，分别是<strong>顺序结构</strong>、<strong>分支结构</strong>和<strong>循环结构</strong>，代表三种代码执行的顺序。</p><p><strong>「分支流程控制」</strong></p><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line">  // 1. 条件成立执行的代码语句</span><br><span class="line">  if (条件表达式) &#123; </span><br><span class="line">  &#125;</span><br><span class="line">  </span><br><span class="line">  // 2.if else 语句</span><br><span class="line">  if (条件表达式) &#123;</span><br><span class="line">      // [如果] 条件成立执行的代码</span><br><span class="line">  &#125; else &#123;</span><br><span class="line">      // [否则] 执行的代码</span><br><span class="line">  &#125;</span><br><span class="line">  </span><br><span class="line">  // 3. if else if 语句(多分支语句)</span><br><span class="line">  // 适合于检查多重条件。</span><br><span class="line">  if (条件表达式1) &#123;</span><br><span class="line">      语句1；</span><br><span class="line">  &#125; else if (条件表达式2)  &#123;</span><br><span class="line">      语句2；</span><br><span class="line">  &#125; else if (条件表达式3)  &#123;</span><br><span class="line">     语句3；</span><br><span class="line">   ....</span><br><span class="line">  &#125; else &#123;</span><br><span class="line">    // 上述条件都不成立执行此处代码</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p><strong>「三元表达式」</strong></p><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">//如果表达式1为 true ，则返回表达式2的值，如果表达式1为 false，则返回表达式3的值</span><br><span class="line">表达式1 ? 表达式2 : 表达式3;</span><br></pre></td></tr></table></figure><p><strong>「switch分支流程控制」</strong>它用于基于不同的条件来执行不同的代码。当要针对变量设置一系列的特定值的选项时，就可以使用 switch。</p><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">switch( 表达式 )&#123; </span><br><span class="line">    case value1:</span><br><span class="line">        // 表达式 等于 value1 时要执行的代码</span><br><span class="line">        break;</span><br><span class="line">    case value2:</span><br><span class="line">        // 表达式 等于 value2 时要执行的代码</span><br><span class="line">        break;</span><br><span class="line">    default:</span><br><span class="line">        // 表达式 不等于任何一个 value 时要执行的代码</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><h3 id="循环与代码规范"><a href="#循环与代码规范" class="headerlink" title="循环与代码规范"></a>循环与代码规范</h3><h4 id="循环"><a href="#循环" class="headerlink" title="循环"></a>循环</h4><table><thead><tr><th align="left">运算符</th><th align="left">描述</th></tr></thead><tbody><tr><td align="left">初始化变量</td><td align="left">通常被用于初始化一个计数器,该表达式可以使用var关键字声明新的变量，这个变量帮我们来记录次数。</td></tr><tr><td align="left">条件表达式</td><td align="left">用于确定每一次循环是否能被执行，如果结果是true就继续循环，否则退出循环</td></tr><tr><td align="left">操作表达式</td><td align="left">每次循环的最后都要执行的表达式。通常用于更新计数器变量的值</td></tr></tbody></table><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">  for(初始化变量; 条件表达式; 操作表达式 )&#123;</span><br><span class="line">    //循环体</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p><strong>「执行流程」</strong></p><ol><li>初始化变量，初始化操作在整个 for 循环只会执行一次。</li><li>执行条件表达式，如果为true，则执行循环体语句，否则退出循环，循环结束。</li><li>执行操作表达式,此时第一轮结束。</li><li>第二轮开始，直接去执行条件表达式（不再初始化变量），如果为 true ，则去执行循环体语句，否则退出循环。</li><li>继续执行操作表达式，第二轮结束。……</li></ol><p><strong>「双重for循环」</strong>循环嵌套是指在一个循环语句中再定义一个循环语句的语法结构，例如在for循环语句中，可以再嵌套一个for 循环，这样的 for 循环语句我们称之为双重for循环。</p><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">  for (外循环的初始; 外循环的条件; 外循环的操作表达式) &#123;</span><br><span class="line">    for (内循环的初始; 内循环的条件; 内循环的操作表达式) &#123;  </span><br><span class="line">       需执行的代码;</span><br><span class="line">   &#125;</span><br><span class="line">&#125;</span><br><span class="line">//for循环打印九九乘法表</span><br><span class="line">  var str = &quot;&quot;;</span><br><span class="line">  for (var i = 1; i &lt;= 9; i++) &#123;</span><br><span class="line">    for (var j = 1; j &lt;= i; j++) &#123;</span><br><span class="line">      str += j + &quot;x&quot; + i + &quot;=&quot; + j * i + &quot;\t&quot;;</span><br><span class="line">    &#125;</span><br><span class="line">    str += &quot;\n&quot;;</span><br><span class="line">  &#125;</span><br><span class="line">  console.log(str);</span><br></pre></td></tr></table></figure><p><strong>「while循环」</strong></p><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">while (条件表达式) &#123;</span><br><span class="line">    // 循环体代码 </span><br><span class="line">&#125;</span><br><span class="line">// 1. 先执行条件表达式，如果结果为 true，则执行循环体代码；</span><br><span class="line">//    如果为 false，则退出循环，执行后面代码</span><br><span class="line">// 2. 执行循环体代码</span><br><span class="line">// 3.  循环体代码执行完毕后，程序会继续判断执行条件表达式，</span><br><span class="line">//计算1-100的累加和</span><br><span class="line">  var i = 1;</span><br><span class="line">  var sum = 0;</span><br><span class="line">  while (i &lt;= 100) &#123;</span><br><span class="line">    sum += i;</span><br><span class="line">    i++;</span><br><span class="line">  &#125;</span><br><span class="line">  console.log(sum);</span><br></pre></td></tr></table></figure><p><strong>「do-while循环」</strong></p><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">do &#123;</span><br><span class="line">    // 循环体代码 - 条件表达式为 true 时重复执行循环体代码</span><br><span class="line">&#125; while(条件表达式);</span><br><span class="line">    // 先执行一次循环体代码，再执行条件表达式</span><br><span class="line">    //计算100以内的偶数累加和</span><br><span class="line">      var i = 1;</span><br><span class="line">      var sum = 0;</span><br><span class="line">      do &#123;</span><br><span class="line">        if (i % 2 == 0) &#123;</span><br><span class="line">          sum += i;</span><br><span class="line">        &#125;</span><br><span class="line">        i++;</span><br><span class="line">      &#125; while (i &lt;= 100);</span><br><span class="line">      console.log(sum);</span><br></pre></td></tr></table></figure><p><strong>「continue、break」</strong><br><code>continue</code> 关键字用于立即跳出本次循环，继续下一次循环（本次循环体中 continue 之后的代码就会少执行一次）。<br><code>break</code> 关键字用于立即跳出整个循环（循环结束）。</p><h3 id="代码规范"><a href="#代码规范" class="headerlink" title="代码规范"></a>代码规范</h3><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">1. 标识符命名规范</span><br></pre></td></tr></table></figure><ul><li>变量、函数的命名必须要有意义</li><li>变量的名称一般用名词</li><li>函数的名称一般用动词</li></ul><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line">2. 操作符规范</span><br><span class="line">// 操作符的左右两侧各保留一个空格</span><br><span class="line">for (var i = 1; i &lt;= 5; i++) &#123;</span><br><span class="line">  if (i == 3) &#123;</span><br><span class="line">      break; // 直接退出整个 for 循环，跳到整个for循环下面的语句</span><br><span class="line">  &#125;</span><br><span class="line">  console.log(&#x27;我正在吃第&#x27; + i + &#x27;个包子呢&#x27;);</span><br><span class="line">&#125;</span><br><span class="line">3. 单行注释规范</span><br><span class="line"> for (var i = 1; i &lt;= 5; i++) &#123;</span><br><span class="line">  if (i == 3) &#123;</span><br><span class="line">      break; // 单行注释前面注意有个空格</span><br><span class="line">  &#125;</span><br><span class="line">  console.log(&#x27;我正在吃第&#x27; + i + &#x27;个包子呢&#x27;);</span><br><span class="line">&#125;</span><br><span class="line">4. 其他规范</span><br><span class="line">//关键词 操作符空格</span><br><span class="line">if (true) &#123;&#125;</span><br><span class="line">for (var i = 0; i&lt;=10; i++) &#123;&#125;</span><br></pre></td></tr></table></figure><h2 id="数组与函数"><a href="#数组与函数" class="headerlink" title="数组与函数"></a>数组与函数</h2><h3 id="数组"><a href="#数组" class="headerlink" title="数组"></a>数组</h3><p><strong>「1. 数组的概念」</strong> 一组数据的集合，其中的每个数据被称作<code>元素</code>，在数组中可以存放任意类型的元素。数组是一种将一组数据存储在单个变量名下的优雅方式。</p><p><strong>「2. 创建数组」</strong></p><ul><li>利用new关键字创建数组；</li></ul><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">var 数组名 = new Array([n]);//[]代表可选 若写n，则代表数组的长度</span><br><span class="line">var arr = new Array();//创建了一个名为 arr 的空数组</span><br></pre></td></tr></table></figure><ul><li>利用数组字面量创建数组</li></ul><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">// 1. 使用数组字面量方式创建空的数组</span><br><span class="line">var 数组名 = [];//若写n，则代表数组的长度</span><br><span class="line"></span><br><span class="line">//2. 使用数组字面量方式创建带初始值的数组</span><br><span class="line">//3. 声明数组并赋值称为数组的初始化</span><br><span class="line">var arr =[&#x27;1&#x27;,&#x27;2&#x27;,&#x27;3&#x27;,&#x27;4&#x27;];</span><br><span class="line">var arr2 = [&#x27;fan&#x27;,true,17.5];//数组中可以存放任意类型的数据</span><br></pre></td></tr></table></figure><p><strong>「3. 访问数组元素」</strong><br>索引(下标):用来访问数组元素的序号。索引从 <code>0</code> 开始<img src="" data-lazy-src="" alt="图片"></p><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">// 定义数组</span><br><span class="line">var arrStus = [1,2,3];</span><br><span class="line">// 获取数组中的第2个元素 </span><br><span class="line">alert(arrStus[1]); </span><br><span class="line">// 如果访问数组时没有和索引值对应的元素(数组越界)，</span><br><span class="line">// 返回值为undefined</span><br></pre></td></tr></table></figure><p><strong>「4. 遍历数组」</strong><br>把数组中的元素从头到尾都访问一次。</p><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">// 数组的长度,默认情况下等于元素的个数</span><br><span class="line">// 当我们数组里面的元素发生了变化，length属性跟着一起变</span><br><span class="line">// 如果设置的length属性值大于数组的元素个数，则会在数组末尾出现空白元素；</span><br><span class="line">// 如果设置的length属性值小于数组的元素个数，则会把超过该值的数组元素删除</span><br><span class="line"> var arr = [&quot;red&quot;, &quot;blue&quot;, &quot;green&quot;];</span><br><span class="line"> for (var i = 0; i &lt; arr.length; i++) &#123;</span><br><span class="line">   console.log(arr[i]);</span><br><span class="line"> &#125;</span><br><span class="line"> </span><br><span class="line"> arr.length = 2;</span><br><span class="line"> console.log(arr);// red blue</span><br></pre></td></tr></table></figure><p><strong>「4. 数组中新增元素」</strong><br>数组中可以通过以下方式在数组的末尾插入新元素;</p><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">// 1. 数组[数组.length] = 新数据;</span><br><span class="line">arr = [] //arr.length = 0;</span><br><span class="line">for (var i = 0; i &lt; 10; i++) &#123;</span><br><span class="line">  arr[arr.length] = &#x27;0&#x27;;</span><br><span class="line">&#125;</span><br><span class="line">console.log(arr);</span><br></pre></td></tr></table></figure><p><strong>「5. 案例」</strong></p><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br></pre></td><td class="code"><pre><span class="line">// 1. 筛选数组 大于10的元素选出来放到新数组中</span><br><span class="line">var arr = [2, 0, 6, 1, 77, 0, 52, 0, 25, 7];</span><br><span class="line">var newArr = [];</span><br><span class="line">var j = 0;</span><br><span class="line">for (var i = 0;i &lt; arr.length; i++)&#123;</span><br><span class="line">  if (arr[i] &gt;= 10) &#123;</span><br><span class="line">    newArr[j] = arr[i];</span><br><span class="line">    j++;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line">console.log(newArr);</span><br><span class="line">//第二种方法 利用数组长度的可变性</span><br><span class="line">for (var i = 0;i &lt; arr.length; i++)&#123;</span><br><span class="line">  if (arr[i] &gt;= 10) &#123;</span><br><span class="line">    newArr[j] = arr[i];</span><br><span class="line">    j++;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line">// 2. 翻转数组 </span><br><span class="line">//  把旧数组索引号第4个取过来（arr.length - 1)，给新数组索引号第0个元素 (newArr.length)</span><br><span class="line">//  我们采取 递减的方式  i--</span><br><span class="line">var arr = [&#x27;red&#x27;, &#x27;green&#x27;, &#x27;blue&#x27;, &#x27;pink&#x27;, &#x27;purple&#x27;];</span><br><span class="line">var newArr = [];</span><br><span class="line">for(var i = arr.length - 1; i &gt;= 0;i--) &#123;</span><br><span class="line">  newArr[newArr.length] = arr[i]</span><br><span class="line">&#125;</span><br><span class="line">console.log(newArr);</span><br><span class="line">// 3. 数组转换为字符串 用&quot;|&quot; 或其他符号分割</span><br><span class="line">// 需要一个新变量用于存放转换完的字符串str</span><br><span class="line">//遍历取出数据加到str后面然后加上分隔符</span><br><span class="line">var arr = [&#x27;red&#x27;, &#x27;green&#x27;, &#x27;blue&#x27;, &#x27;pink&#x27;, &#x27;purple&#x27;];</span><br><span class="line">var str = &#x27;&#x27;;</span><br><span class="line">for(var i = 0; i &lt; arr.length; i++) &#123;</span><br><span class="line">  str += arr[i] + &#x27;|&#x27;;</span><br><span class="line">&#125;</span><br><span class="line">console.log(str);</span><br><span class="line">// 4. 数组转换为字符串 用&quot;|&quot; 或其他符号分割</span><br><span class="line">// 需要一个新变量用于存放转换完的字符串str</span><br><span class="line">//遍历取出数据加到str后面然后加上分隔符</span><br><span class="line">var arr = [&#x27;red&#x27;, &#x27;green&#x27;, &#x27;blue&#x27;, &#x27;pink&#x27;, &#x27;purple&#x27;];</span><br><span class="line">var str = &#x27;&#x27;;</span><br><span class="line">for(var i = 0; i &lt; arr.length; i++) &#123;</span><br><span class="line">  str += arr[i] + &#x27;|&#x27;;</span><br><span class="line">&#125;</span><br><span class="line">console.log(str);</span><br></pre></td></tr></table></figure><p><code>冒泡排序</code>![图片](<a target="_blank" rel="noopener" href="https://cdn1.tianli0.top/gh/web-xc/iMG/posts/xuexijs">https://cdn1.tianli0.top/gh/web-xc/iMG/posts/xuexijs</a> (9).png)</p><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">function sort(arr) &#123;</span><br><span class="line">  for(var i = 0; i &lt; arr.length - 1; i++) &#123;</span><br><span class="line">    for(var j = 0; j &lt; arr.length - 1 - i; j++) &#123;</span><br><span class="line">      if (arr[j] &gt; arr[j+1])&#123;</span><br><span class="line">        var temp = arr[j];</span><br><span class="line">        arr[j] = arr[j+1];</span><br><span class="line">        arr[j+1] = temp;</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">  return arr;</span><br><span class="line">&#125;</span><br><span class="line">var arr1 = sort([1,4,2,9]); </span><br><span class="line">console.log(arr1);//1 2 4 9</span><br></pre></td></tr></table></figure><h3 id="函数"><a href="#函数" class="headerlink" title="函数"></a>函数</h3><p><strong>「1. 函数的概念」</strong><br>封装了一段可被重复调用执行的代码块，通过函数可以实现大量代码的重复使用。函数是一种数据类型。</p><p><strong>「2. 函数的使用」</strong></p><ul><li>声明函数</li></ul><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">1. 通过function关键字定义函数 -- 命名函数</span><br><span class="line">function 函数名() &#123;</span><br><span class="line">  //函数体代码</span><br><span class="line">&#125;</span><br><span class="line">// 1.1 function 是声明函数的关键字，必须小写</span><br><span class="line">// 1.2 函数名 命名为动词形式 例: getSum</span><br><span class="line"></span><br><span class="line"> 2. 通过函数表达式定义函数 ---匿名函数</span><br><span class="line">var fn = function() &#123;&#125;;</span><br><span class="line">// 2.1 fn是变量名，不是函数名</span><br><span class="line">// 2.2 fn是变量，只不过变量存储的是函数</span><br><span class="line">// 2.3 函数表达式创建的函数可以通过 变量名(); 来调用</span><br><span class="line">// 2.4 函数表达式也可以定义形参和调用传入实参。</span><br><span class="line">匿名函数使用的第2种方式--匿名函数自调用</span><br><span class="line">(function()&#123;</span><br><span class="line">  alert(123);</span><br><span class="line">&#125;)();</span><br></pre></td></tr></table></figure><ul><li>调用函数</li></ul><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">函数名();// 函数声明后调用才会执行函数体代码</span><br></pre></td></tr></table></figure><ul><li><p>函数的封装</p></li><li><ul><li>函数的封装是把一个或者多个功能通过函数的方式封装起来，对外只提供一个简单的函数接口。</li></ul></li></ul><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">/*</span><br><span class="line">  例用封装函数计算1-100累加和</span><br><span class="line">*/</span><br><span class="line">function getSum() &#123;</span><br><span class="line">var sumNum = 0; // 准备一个变量，保存累加和</span><br><span class="line">for (var i = 1; i &lt;= 100; i++) &#123;</span><br><span class="line">  sumNum += i; // 把每个数值 都累加 到变量中</span><br><span class="line">&#125;</span><br><span class="line">  alert(sumNum);</span><br><span class="line">&#125;</span><br><span class="line">// 调用函数</span><br><span class="line">getSum();</span><br></pre></td></tr></table></figure><p><strong>「3. 函数的参数」</strong></p><ul><li>形参：函数定义时候，传递的参数(实参值传递给形参,不用声明的变量)</li><li>实参：函数调用时候，传递的参数</li></ul><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">//带参数的函数声明</span><br><span class="line">function 函数名(形参1,形参2,形参3...) &#123;</span><br><span class="line">  //函数体</span><br><span class="line">&#125;</span><br><span class="line">// 带参数的函数调用</span><br><span class="line">函数名(实参1,实参2,实参3...);</span><br></pre></td></tr></table></figure><ul><li><strong>「函数形参和实参数量不匹配时」</strong></li></ul><table><thead><tr><th align="left">参数个数</th><th align="left">说明</th></tr></thead><tbody><tr><td align="left">实参个数等于形参个数</td><td align="left">输出正确结果</td></tr><tr><td align="left">实参个数多于形参个数</td><td align="left">只取到形参的个数</td></tr><tr><td align="left">实参个数小于形参</td><td align="left">多的形参定义为undefined,结果为NaN</td></tr></tbody></table><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">function getSum(a, b, c) &#123;</span><br><span class="line">  return a + b + c;</span><br><span class="line">&#125;</span><br><span class="line">// js中形参的默认值是undefined。</span><br><span class="line">// 调用函数</span><br><span class="line">var n = getSum(1, 2);// n = NaN</span><br><span class="line">var n = getSum(1, 2, 3, 4); //1 + 2 +3 = 6</span><br></pre></td></tr></table></figure><p><strong>「4. 函数的返回值」</strong><br>返回值：函数调用整体代表的数据；函数执行完成后可以通过return语句将指定数据返回 。</p><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">  // 声明函数</span><br><span class="line">  function 函数名() &#123;</span><br><span class="line">    ...</span><br><span class="line">    return 需要返回的值;</span><br><span class="line">    // 1. 函数遇到return会停止执行，并返回指定的值</span><br><span class="line">    // 1. 如果函数没有return 返回的值是undefined</span><br><span class="line">  &#125;</span><br><span class="line">  // 调用函数</span><br><span class="line">  函数名(); //此时调用函数就可以得到函数体内return的值</span><br><span class="line">break,continue,return的区别</span><br></pre></td></tr></table></figure><ul><li>break: 结束当前的循环体 (如for、while)</li><li>continue: 跳出本次循环，继续执行下次循环</li><li>return: 不仅可以退出<code>(函数体内)</code>循环，还能够返回return语句中的值，同时还可以<code>结束当前的函数体内的代码</code></li></ul><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">//避免踩坑 return只能结束函数体内的代码</span><br><span class="line">  function breakDown() &#123;</span><br><span class="line">    for (var i = 0; i &lt; 10; i++) &#123;</span><br><span class="line">      if (i == 5) &#123;</span><br><span class="line">        return 1;</span><br><span class="line">      &#125;</span><br><span class="line">    console.log(i);</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">  breakDown();</span><br><span class="line">  </span><br><span class="line">  //避免踩坑2 函数如果有return 则返回的是 return 后面的值；</span><br><span class="line">  // return d,a,b; 返回的是b的值</span><br><span class="line">  //如果函数没有 return语句，则返回undefined</span><br><span class="line"></span><br></pre></td></tr></table></figure><p><strong>「5. arguments的使用」</strong><br>当不确定有多少个参数传递的时候，可以用 arguments 来获取。JS中，arguments实际上它是当前函数的一个内置对象。所有函数都内置了一个 arguments 对象，arguments 对象中存储了传递的所有实参。arguments展示形式是一个伪数组，因此可以进行遍历。</p><ul><li><p>伪数组具有以下特点：</p></li><li><ul><li>具有length属性</li><li>按索引方式存储数据</li><li>不具有数组的push，pop等方法</li></ul></li></ul><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"> <span class="keyword">function</span> <span class="title function_">fn</span>(<span class="params"></span>) &#123;</span><br><span class="line">   <span class="comment">//arguments 里面存储了所有传递过来的实参</span></span><br><span class="line">   <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="variable language_">arguments</span>);<span class="comment">// [1,2,3...]</span></span><br><span class="line">   <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="variable language_">arguments</span>[<span class="number">1</span>]); <span class="comment">// 2</span></span><br><span class="line">   <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="variable language_">arguments</span>.<span class="property">length</span>); <span class="comment">// 3</span></span><br><span class="line">   </span><br><span class="line">   <span class="comment">//我们可以按照数组的方式 遍历argument  </span></span><br><span class="line"> &#125;</span><br><span class="line"> <span class="title function_">fn</span>(<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>);</span><br><span class="line"><span class="comment">// 用伪数组 实现求最大值</span></span><br><span class="line"><span class="keyword">function</span> <span class="title function_">getMax</span>(<span class="params"></span>) &#123;</span><br><span class="line">   <span class="keyword">var</span> max = <span class="variable language_">arguments</span>[<span class="number">0</span>];</span><br><span class="line">   <span class="keyword">for</span> (<span class="keyword">var</span> i = <span class="number">1</span>; i &lt; <span class="variable language_">arguments</span>.<span class="property">length</span>; i++) &#123;</span><br><span class="line">     <span class="keyword">if</span> (<span class="variable language_">arguments</span>[i] &gt; <span class="variable language_">arguments</span>[<span class="number">0</span>]) &#123;</span><br><span class="line">       max = <span class="variable language_">arguments</span>[i];</span><br><span class="line">     &#125;</span><br><span class="line">   &#125;</span><br><span class="line">   <span class="keyword">return</span> max;</span><br><span class="line"> &#125;</span><br><span class="line"> <span class="keyword">var</span> result = <span class="title function_">getMax</span>(<span class="number">1</span>,<span class="number">3</span>,<span class="number">77</span>,<span class="number">5</span>,<span class="number">85</span>)</span><br><span class="line"> colsole.<span class="title function_">log</span>(result);</span><br></pre></td></tr></table></figure><hr><h2 id="作用域"><a href="#作用域" class="headerlink" title="作用域"></a>作用域</h2><h3 id="作用域-1"><a href="#作用域-1" class="headerlink" title="作用域"></a>作用域</h3><p><strong>「作用域」</strong>一段程序代码中所用到的名字并不总是有效和可靠的，而限定这个名字的可用性代码范围就是这个名字的作用域。</p><ul><li>作用域的使用提高了程序逻辑的局部性，增强了程序的可靠性，减少了名字冲突。</li><li>ES6之前作用域有两种 <code>全局作用域</code>和<code>局部作用域</code>(函数作用域)</li></ul><p><strong>「全局作用域」</strong>作用于所有代码执行的环境(整个 script 标签内部)或者一个独立的js文件。</p><p><strong>「局部作用域」</strong>作用于函数内部的代码环境，就是局部作用域。因为跟函数有关系，所以也被称为<code>函数作用域</code>。</p><p><strong>「JS没有块级作用域」</strong></p><ul><li>块作用域由 {} 包括</li><li>在其他编程语言，if语句中，循环语句创建的变量，仅仅只能在本if语句，本循环语句中使用，如下👇👇</li></ul><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">if(true)&#123;</span><br><span class="line">  int num = 123;</span><br><span class="line">  System.out.print(num); //123</span><br><span class="line">&#125;</span><br><span class="line">System.out.print(num);//报错</span><br></pre></td></tr></table></figure><ul><li>以上java代码会报错，因为代码中 {}是一块作用域，其中声明的变量num，在{}之外不能使用，而JavaScript代码则不会报错</li><li>Js中没有块级作用域 (在ES6之前)</li></ul><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">if(true)&#123;</span><br><span class="line">  var num = 123;</span><br><span class="line">  console.log(num); // 123</span><br><span class="line">&#125;</span><br><span class="line">console.log(num);// 123</span><br></pre></td></tr></table></figure><h3 id="变量的作用域"><a href="#变量的作用域" class="headerlink" title="变量的作用域"></a>变量的作用域</h3><blockquote><p>在JavaScript中，根据作用域的不同，变量可以分为两种:👇</p><ul><li><code>全局变量</code></li><li><code>局部变量</code></li></ul></blockquote><p><strong>「全局变量」</strong>在全局作用域下声明的变量(在函数外部定义的变量)</p><ul><li>全局变量在代码的任何位置都可以使用</li><li>在全局作用域下 var 声明的变量 是全局变量</li><li>特殊情况下，在函数内不使用var声明的变量也是全局变量(不建议使用)。</li></ul><p><strong>「局部变量」</strong>在局部作用域下声明的变量(在函数内部定义的变量)</p><ul><li>局部变量只能在函数内部使用</li><li>在函数内部 var声明的变量是局部变量</li><li>函数的形参实际上就是局部变量</li></ul><p><strong>「全局变量和局部变量的区别」</strong></p><ul><li><code>全局变量:</code>在任何一个地方都可以使用，只有在浏览器关闭时才会销毁，因此比较占内存</li><li><code>局部变量:</code>旨在函数内部使用，当其所在的代码块被执行时，才会被初始化；当代码块运行结束后，就会被销毁，因此更节省内存空间。</li></ul><h3 id="作用域链"><a href="#作用域链" class="headerlink" title="作用域链"></a>作用域链</h3><p><strong>「作用域链」</strong>只要是代码都在一个作用域中，写在函数内部的局部作用域，未卸载仍和行数内部即在全局作用域中；如果函数中还有函数，那么在这个作用域中就又可以诞生一个作用域；根据<code>[内部函数可以访问外部函数变量]</code> 的这种机制，用链式查找决定哪些数据能被内部函数访问，就称作作用域链。</p><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">function f1() &#123;</span><br><span class="line">    var num = 123;</span><br><span class="line">    function f2() &#123;</span><br><span class="line">        console.log( num );</span><br><span class="line">    &#125;</span><br><span class="line">    f2();</span><br><span class="line">&#125;</span><br><span class="line">var num = 456;</span><br><span class="line">f1();</span><br></pre></td></tr></table></figure><p><img src="" data-lazy-src="" alt="图片"></p><p><code>作用域链</code>采取就近原则的方式来查找变量最终的值</p><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line">var a = 1;</span><br><span class="line">function fn1() &#123;</span><br><span class="line">    var a = 2;</span><br><span class="line">    var b = &#x27;22&#x27;;</span><br><span class="line">    fn2();</span><br><span class="line">    function fn2() &#123;</span><br><span class="line">        var a = 3;</span><br><span class="line">        fn3();</span><br><span class="line">        function fn3() &#123;</span><br><span class="line">            var a = 4;</span><br><span class="line">            console.log(a); //a的值 4</span><br><span class="line">            console.log(b); //b的值 &#x27;22&#x27;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">fn1();</span><br></pre></td></tr></table></figure><p>![图片](<a target="_blank" rel="noopener" href="https://cdn1.tianli0.top/gh/web-xc/iMG/posts/xuexijs">https://cdn1.tianli0.top/gh/web-xc/iMG/posts/xuexijs</a> (8).png)</p><h3 id="预解析"><a href="#预解析" class="headerlink" title="预解析"></a>预解析</h3><p><strong>「预解析相关概念」</strong>JavaScript代码是由浏览器中的JavaScript解析器来执行的。JavaScript解析器在运行JavaScript代码的时候分为两步:预解析和代码执行。</p><ul><li><strong>「预解析」</strong>在当前作用域下，JS代码执行之前，浏览器会默认把带有 var 和 function声明的变量在内存中进行提前声明或定义。</li><li><strong>「代码执行」</strong>从上往下执行JS语句</li></ul><p>预解析会把变量和函数的声明在代码执行之前完成，预解析也叫做变量、函数提升。</p><p><strong>「变量预解析(变量提升)」</strong> 变量的声明会被提升到当前作用域的最上面，变量的赋值不提升。</p><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">console.log(num);  // 结果是多少？</span><br><span class="line">var num = 10;      // ？</span><br><span class="line"></span><br><span class="line">相当于</span><br><span class="line">var num;</span><br><span class="line">console.log(num);// 结果是 undefined</span><br><span class="line">num = 10;</span><br><span class="line">结果: undefined</span><br><span class="line">注意: 变量提升只提升声明，不提升赋值。</span><br></pre></td></tr></table></figure><p><strong>「函数预解析(函数提升)」</strong> 函数的声明会被提升到当前作用域的最上面，但是不会调用函数。</p><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">fn();</span><br><span class="line">function fn() &#123;</span><br><span class="line">    console.log(&#x27;打印&#x27;);</span><br><span class="line">&#125;</span><br><span class="line">结果: 控制台打印字符串 --- &quot;打印&quot;</span><br><span class="line">注意: 函数声明代表函数整体，所以函数提升后，函数名代表整个函数，但是函数并没有被调用！</span><br></pre></td></tr></table></figure><p><strong>「函数表达式声明函数问题」</strong></p><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br></pre></td><td class="code"><pre><span class="line">函数表达式创建函数，会执行变量提升，此时接收函数的变量名无法正确的调用</span><br><span class="line">fn();</span><br><span class="line">var fn = function()&#123;</span><br><span class="line">  console.log(&quot;想不到吧&quot;);</span><br><span class="line">&#125;</span><br><span class="line">结果:报错提示 &quot;fn is not a function&quot;</span><br><span class="line">解释: 该段代码执行之前，会做变量声明提升，fn在提升之后的值</span><br><span class="line">      是undefined;而fn调用是在fn被赋值为函数体之前，</span><br><span class="line">      此时fn的值是undefined，所以无法被调用。</span><br><span class="line">预解析案例1</span><br><span class="line">var num = 10;</span><br><span class="line">fun();</span><br><span class="line"></span><br><span class="line">function fun()&#123;</span><br><span class="line">  console.log(num);</span><br><span class="line">  var num = 20;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">相当于执行了以下操作 结果打印 undefined</span><br><span class="line">var num;</span><br><span class="line"></span><br><span class="line">function fun()&#123;</span><br><span class="line">  var num;</span><br><span class="line">  console.log(num);</span><br><span class="line">  num = 20;</span><br><span class="line">&#125;</span><br><span class="line">num = 10;</span><br><span class="line">fun(); </span><br><span class="line">预解析案例2</span><br><span class="line">var a = 18;</span><br><span class="line">f1();</span><br><span class="line"></span><br><span class="line">function f1()&#123;</span><br><span class="line">  var b = 9;</span><br><span class="line">  console.log(a);</span><br><span class="line">  console.log(b);</span><br><span class="line">  var a = &#x27;123&#x27;;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">相当于执行了以下操作 结果为 undefined 9</span><br><span class="line">var a;</span><br><span class="line">function f1()&#123;</span><br><span class="line">  var b;</span><br><span class="line">  var a;</span><br><span class="line">  b = 9;</span><br><span class="line">  console.log(a);</span><br><span class="line">  console.log(b);</span><br><span class="line">  a = &#x27;123&#x27;;  </span><br><span class="line">&#125;</span><br><span class="line">a = 18;</span><br><span class="line">f1();  </span><br><span class="line">预解析案例3</span><br><span class="line">f1();</span><br><span class="line">console.log(c);</span><br><span class="line">console.log(b);</span><br><span class="line">console.log(a);</span><br><span class="line"></span><br><span class="line">function f1() &#123;</span><br><span class="line">  var a = b = c = 9;</span><br><span class="line">  console.log(a);</span><br><span class="line">  console.log(b);</span><br><span class="line">  console.log(c);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">相当于执行了以下操作 结果为 9 9 9 9 9 &quot;报错--a is not defined&quot;</span><br><span class="line">function f1() &#123;</span><br><span class="line">  var a;</span><br><span class="line">  a = b = c = 9;</span><br><span class="line">  //相当于 var a = 9; b=9; c=9; b和c 直接赋值，没有var声明，当全局变量看。</span><br><span class="line">  // 差异：集体声明 var a = 9,b = 9, c = 9;</span><br><span class="line">  console.log(a);</span><br><span class="line">  console.log(b);</span><br><span class="line">  console.log(c);</span><br><span class="line">&#125;</span><br><span class="line">f1();</span><br><span class="line">console.log(c);</span><br><span class="line">console.log(b);</span><br><span class="line">console.log(a);</span><br></pre></td></tr></table></figure><hr><h3 id="对象"><a href="#对象" class="headerlink" title="对象"></a>对象</h3><h4 id="对象的概念"><a href="#对象的概念" class="headerlink" title="对象的概念"></a>对象的概念</h4><p><strong>「对象」</strong>在JavaScript中，对象是一组无序的相关属性和方法的集合，所有的事物都是对象，例如字符串、数值、数组、函数等。</p><ul><li><p>对象是由属性和方法组成的</p></li><li><ul><li>属性：事物的特征，在对象中用属性来表示（常用名词）</li><li>方法：事物的行为，在对象中常用方法来表示（常用动词）</li></ul></li></ul><p><strong>「为什么需要对象」</strong></p><ul><li>保存一个值时，可以使用变量，保存多个值（一组值）时，可以使用数组，如果保存一个的完整信息呢？</li><li>为了更好地存储一组数据，对象应用而生；对象中为每项数据设置了属性名称，可以访问数据更语义化，数据结构清晰，表意明显，方便开发者使用。</li></ul><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">var obj = &#123;</span><br><span class="line">  &quot;name&quot;:&quot;fan&quot;,</span><br><span class="line">  &quot;sex&quot;:&quot;male&quot;,</span><br><span class="line">  &quot;age&quot;:18,</span><br><span class="line">  &quot;height&quot;:155</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><h4 id="创建对象的三种方式"><a href="#创建对象的三种方式" class="headerlink" title="创建对象的三种方式"></a>创建对象的三种方式</h4><p><strong>「1. 利用字面量创建对象」</strong>使用对象字面量创建对象：</p><ul><li>就是花括号 { } 里面包含了表达这个具体事物（对象）的属性和方法；{ } 里面采取键值对的形式表示<br>-<code>键</code>：相当于属性名<br>-<code>值</code>：相当于属性值，可以是任意类型的值（数字类型、字符串类型、布尔类型，函数类型等）</li></ul><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">// star 就是创建的对象</span><br><span class="line">var star = &#123;</span><br><span class="line">  name : &#x27;pink&#x27;,</span><br><span class="line">  age : 18,</span><br><span class="line">  sex : &#x27;男&#x27;,</span><br><span class="line">  sayHi : function() &#123;</span><br><span class="line">    alert(&#x27;大家好啊&#x27;);</span><br><span class="line">  &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure><ul><li><p>对象的使用</p></li><li><ul><li>对象的属性：对象中<code>存储具体数据</code>的“键值对”中的键称为对象的属性，即对象中存储具体数据的项。</li><li>对象的方法：对象中<code>存储函数</code>的“键值对”中的“键”称为对象的方法，即对象中存储函数的项。</li><li>访问对象的属性：对象里面的属性调用：<code>对象.属性名</code>；对象里面属性的另一种调用方式：对象[‘属性名’],注意方括号里面的属性必须加上引号。</li><li>调用对象的方法:<code>对象.方法名();</code></li><li>变量、属性、函数、方法总结：<br><code>①变量</code>：单独声明赋值，单独存在<br><code>②属性</code>:对象里面的变量称为属性，不需要声明，用来描述该对象的特征。<br><code>③方法</code>：方法是对象的一部分，函数不是对象的一部分，函数是单独封装操作的容器。对象里面的函数称为方法，方法不需要声明，使用”对象.方法名()”的方式就可以调用，方法用来描述该对象的行为和功能。<br><code>④函数</code>:单独存在的，通过”函数名()”的方式就可以调用。</li></ul></li></ul><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">console.log(star.name)     // 调用名字属性</span><br><span class="line">console.log(star[&#x27;name&#x27;])  // 调用名字属性</span><br><span class="line"></span><br><span class="line">star.sayHi();</span><br></pre></td></tr></table></figure><p><strong>「2. 利用new Object创建对象」</strong></p><ul><li>创建空对象</li></ul><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">通过内置构造函数Object创建对象，此时andy变量已经保存了创建出来的空对象</span><br><span class="line">var andy = new Object();  </span><br></pre></td></tr></table></figure><ul><li>给空对象添加属性和方法</li></ul><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">通过对象操作属性和方法的方式，来为对象增加属性和方法</span><br><span class="line">andy.name = &#x27;pink&#x27;;</span><br><span class="line">andy.age = 18; // andy.age = 19修改对象属性</span><br><span class="line">andy.sex = &#x27;男；// andy.phoneNum = 110 添加属性</span><br><span class="line">andy.sayHi = function() &#123;</span><br><span class="line">  alert(&#x27;大家好&#x27;);</span><br><span class="line">&#125;</span><br><span class="line">obj.sayHi();调用对象的方法 //第二种写法 obj[&#x27;sayHi&#x27;]();</span><br><span class="line"></span><br><span class="line">// Object()第一个字母大写；</span><br><span class="line">//new Object() 需要new关键字，使用的格式:对象.属性 = 值</span><br></pre></td></tr></table></figure><p><strong>「3. 利用构造函数创建对象」</strong></p><p><code>构造函数</code>是一种特殊的函数，主要用来初始化对象，即为对象成员变量赋初始值,它总与new运算符一起使用，我们可以把对象中一些公共的属性和方法抽出来，然后封装到这个函数里面。</p><ul><li>构造函数的封装格式：</li></ul><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">function 构造函数名(形参1，形参2，形参3...) &#123;</span><br><span class="line">  this.属性名1 = 参数1;</span><br><span class="line">  this.属性名2 = 参数2;</span><br><span class="line">  this.属性名3 = 参数3;</span><br><span class="line">  this.方法名 = 函数体;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><ul><li>构造函数的调用格式</li></ul><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">var obj = new 构造函数名(实参1，实参2，实参3);</span><br><span class="line">// 以上代码中，obj即接收到构造函数创建出来的对象。</span><br><span class="line">注意事项：</span><br><span class="line">  1.构造函数约定首字母大写</span><br><span class="line">  2.函数内的属性和方法前面需要添加this，表示当前对象的属性和方法</span><br><span class="line">  3.构造函数中不需要retrun返回结果</span><br><span class="line">  4.但我们创建对象的时候，必须用new 来调用构造函数</span><br><span class="line">  </span><br><span class="line">  1.其他：构造函数如Stars(),抽取了对象的公共部分，封装到了函数里面，它</span><br><span class="line">   泛指某一大类(class)</span><br><span class="line">  2.创建对象，如new Stars();特指某一个，利用new关键字创建对象的过程</span><br><span class="line">   我们也称为对象实例化</span><br><span class="line"> </span><br></pre></td></tr></table></figure><ul><li><p><strong>new关键字的作用(面试题)</strong></p></li><li><ul><li>1.在构造函数代码开始执行之前，创建一个空对象；</li><li>2.修改this的指向，把this指向创建出来的空对象；</li><li>3.执行构造函数内的代码，给这个新对象添加属性和方法</li><li>4.在函数完成之后，返回这个创建出来的新对象(所以构造函数里面不需要return)</li></ul></li></ul><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">// 工厂函数创建对象 这个把创建好的对象返回给函数调用处</span><br><span class="line">function createPerson(name, age, job) &#123;</span><br><span class="line">  var person = new Object();</span><br><span class="line">  person.name = name;</span><br><span class="line">  person.age = age;</span><br><span class="line">  person.job = job;</span><br><span class="line">  person.sayHi = function()&#123;</span><br><span class="line">    console.log(&#x27;Hello,everyBody&#x27;);</span><br><span class="line">  &#125;</span><br><span class="line">  return person;</span><br><span class="line">  &#125;</span><br><span class="line">var p1 = createPerson(&#x27;张三&#x27;, 22, &#x27;actor&#x27;);</span><br></pre></td></tr></table></figure><h3 id="遍历对象"><a href="#遍历对象" class="headerlink" title="遍历对象"></a>遍历对象</h3><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">  for...in 语句用于对数组或者对象的属性进行循环操作。</span><br><span class="line"></span><br><span class="line">  其语法如下：</span><br><span class="line">  for (变量 in 对象名字) &#123;</span><br><span class="line">      // 在此执行代码</span><br><span class="line">  &#125;</span><br><span class="line">  语法中的变量是自定义的，它需要符合命名规范，通常我们会将这个变量写为 k 或者 key。</span><br><span class="line">  </span><br><span class="line">  for (var k in obj) &#123;</span><br><span class="line">    console.log(k);      // 这里的 k 是属性名</span><br><span class="line">    console.log(obj[k]); // 这里的 obj[k] 是属性值</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><hr><h2 id="内置对象"><a href="#内置对象" class="headerlink" title="内置对象"></a>内置对象</h2><p><strong>「内置对象」</strong>JavaScript 中的对象分为3种：自定义对象 、内置对象、 浏览器对象<br>前面两种对象是JS基础内容，属于ECMAScript;第三个浏览器对象属于JS独有的，JS API讲解内置对象就是指js语言自带的一些对象，这些对象供开发者使用，并提供了一些常用的或是最基本而非必要的功能(属性和方法),内置对象最大的优点就是帮助我们快速开发。</p><p><strong>「查文档」</strong>学习一个内置对象的使用，只要学会其常用成员的使用即可，我们可以通过查文档学习。<br><code>MDN:</code><a target="_blank" rel="noopener" href="https://developer.mozilla.org/zh-CN/">https://developer.mozilla.org/zh-CN/</a></p><h3 id="Math对象"><a href="#Math对象" class="headerlink" title="Math对象"></a>Math对象</h3><p><strong>「Math对象」</strong>不是构造函数，它具有数学常数和函数的属性和方法，跟数学相关。</p><table><thead><tr><th align="left">属性、方法名</th><th align="left">功能</th></tr></thead><tbody><tr><td align="left">Math.PI</td><td align="left">圆周率</td></tr><tr><td align="left">Math.floor()</td><td align="left">向下取整</td></tr><tr><td align="left">Math.ceil()</td><td align="left">向上取整</td></tr><tr><td align="left">Math.round()</td><td align="left">四舍五入版 就近取整 注意 -3.5 结果是 -3</td></tr><tr><td align="left">Math.abs()</td><td align="left">绝对值</td></tr><tr><td align="left">Math.max()&#x2F;Math.min()</td><td align="left">求最大和最小值</td></tr><tr><td align="left">Math.random()</td><td align="left">获取范围在[0,1)内的随机值</td></tr></tbody></table><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">- 获取指定范围的随机整数</span><br><span class="line">  function getRandom(min, max) &#123;</span><br><span class="line">    return Math.floor(Math.random() * (max - min + 1)) + min; </span><br><span class="line">  &#125;</span><br></pre></td></tr></table></figure><h3 id="日期对象"><a href="#日期对象" class="headerlink" title="日期对象"></a>日期对象</h3><p>Date 对象和 Math 对象不一样，Date是一个构造函数，所以使用时需要实例化后才能使用其中具体方法和属性。Date 实例用来处理日期和时间</p><ul><li><p><strong>使用Date实例化日期对象</strong></p></li><li><ul><li>获取当前时间必须实例化</li><li>获取指定时间的日期对象</li></ul></li></ul><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">var now = new Date();</span><br><span class="line">var future = new Date(&#x27;2020/10/1&#x27;)</span><br><span class="line">// 注意：如果创建实例时并未传入参数，则得到的日期对象是当前时间对应的日期对象</span><br></pre></td></tr></table></figure><ul><li><p>续上</p></li><li><ul><li>使用Date实例的方法和属性<ul><li>getMonth()方法获取到的月份 + 1 &#x3D; 当月</li></ul></li></ul></li></ul><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br></pre></td><td class="code"><pre><span class="line">//参数常用写法 数字型或者字符串型 &#x27;2019-10-1 8:8:8&#x27;</span><br><span class="line">var date1 = new Date(2019,10,1);</span><br><span class="line">//日期格式化</span><br><span class="line">      // 格式化日期 年 月 日</span><br><span class="line">    var date = new Date();</span><br><span class="line">    console.log(date.getFullYear()); //返回当前日期的年 2020</span><br><span class="line">    console.log(date.getMonth() + 1); //月份 返回的月份小1个月，记得月份加1呦</span><br><span class="line">    console.log(date.getDate()); //返回的是 几号</span><br><span class="line">    console.log(date.getDay); //周一返回的是1 周六返回的是6 周日返回的是0</span><br><span class="line">    //我们写一个 2020年 9月 6日 星期日</span><br><span class="line">    var year = date.getFullYear();</span><br><span class="line">    var month = date.getMonth() + 1;</span><br><span class="line">    var dates = date.getDate();</span><br><span class="line">    var day = date.getDay();</span><br><span class="line">    if (day == 0) &#123;</span><br><span class="line">      day = &quot;星期日&quot;;</span><br><span class="line">    &#125;</span><br><span class="line">    console.log(&quot;今天是&quot; + year + &quot;年&quot; + month + &quot;月&quot; + dates + &quot;日&quot; + day);</span><br><span class="line">    //格式化日期 时分秒</span><br><span class="line">    var date = new Date();</span><br><span class="line">    console.log(date.getHours()); //时</span><br><span class="line">    console.log(date.getMinutes()); //分</span><br><span class="line">    console.log(date.getSeconds()); // 秒</span><br><span class="line"></span><br><span class="line">    //封装一个函数返回当前的 时 分 秒 格式 08:08:08</span><br><span class="line">    function getTimer() &#123;</span><br><span class="line">      var time = new Date();</span><br><span class="line">      var h = time.getHours();</span><br><span class="line">      var h = h &lt; 10 ? &quot;0&quot; + h : h;</span><br><span class="line"></span><br><span class="line">      var m = time.getMinutes();</span><br><span class="line">      var m = m &lt; 10 ? &quot;0&quot; + m : m;</span><br><span class="line"></span><br><span class="line">      var s = time.getSeconds();</span><br><span class="line">      var s = s &lt; 10 ? &quot;0&quot; + s : s;</span><br><span class="line">      return h + &quot;:&quot; + h + &quot;:&quot; + s;</span><br><span class="line">    &#125;</span><br><span class="line">    console.log(getTimer());</span><br></pre></td></tr></table></figure><ul><li><code>获取Date日期总的毫秒数(时间戳)</code><br>基于1970年1月1日(世界标准世界)起的毫秒数</li></ul><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br></pre></td><td class="code"><pre><span class="line">// 实例化Date对象</span><br><span class="line">var now = new Date();</span><br><span class="line">// 1. 用于获取对象的原始值</span><br><span class="line">console.log(now.valueOf()) </span><br><span class="line">console.log(now.getTime()) </span><br><span class="line">// 2. 简单写可以这么做 (最常用的)</span><br><span class="line">var now = + new Date();   </span><br><span class="line">// 3. HTML5中提供的方法，有兼容性问题</span><br><span class="line">var now = Date.now();</span><br><span class="line">倒计时案例 ：</span><br><span class="line">1. 输入的时间减去现在的时间就是剩余的时间，即倒计时。</span><br><span class="line">2.用时间戳来做，用户输入时间总的毫秒数减去现在时间的总的毫秒数，</span><br><span class="line"> 得到的就是剩余时间的毫秒数</span><br><span class="line">3.把剩余时间总的毫秒数转换为天、时、分、秒  (时间戳转换时分秒)</span><br><span class="line">  转换公式如下：</span><br><span class="line">  d = parseInt(总秒数/60/60/24) // 计算天数</span><br><span class="line">  h = parseInt(总秒数/60/60%24) // 计算小时</span><br><span class="line">  m = parseInt(总秒数/60%60);   // 计算分钟  </span><br><span class="line">  s = parseInt(总秒数%60);      // 计算当前秒数 </span><br><span class="line">    // 倒计时案例 封装函数实现</span><br><span class="line">    function countDown(time) &#123;</span><br><span class="line">      var nowTime = +new Date(); // 返回的是当前时间总的毫秒数</span><br><span class="line">      var inputTime = +new Date(time); // 返回的是用户输入时间总的毫秒数</span><br><span class="line">      var times = (inputTime - nowTime) / 1000; // times是剩余时间总的秒数</span><br><span class="line">      var d = parseInt(times / 60 / 60 / 24); // 天</span><br><span class="line">      d = d &lt; 10 ? &quot;0&quot; + d : d;</span><br><span class="line">      var h = parseInt((times / 60 / 60) % 24); //时</span><br><span class="line">      h = h &lt; 10 ? &quot;0&quot; + h : h;</span><br><span class="line">      var m = parseInt((times / 60) % 60); // 分</span><br><span class="line">      m = m &lt; 10 ? &quot;0&quot; + m : m;</span><br><span class="line">      var s = parseInt(times % 60); // 当前的秒</span><br><span class="line">      s = s &lt; 10 ? &quot;0&quot; + s : s;</span><br><span class="line">      return d + &quot;天&quot; + h + &quot;时&quot; + m + &quot;分&quot; + s + &quot;秒&quot;;</span><br><span class="line">    &#125;</span><br><span class="line">    console.log(countDown(&quot;2020-10-1 18:00:00&quot;));</span><br><span class="line">    var date = new Date();</span><br><span class="line">    console.log(date);</span><br></pre></td></tr></table></figure><h3 id="数组对象"><a href="#数组对象" class="headerlink" title="数组对象"></a>数组对象</h3><p><strong>「创建数组的两种方式」</strong></p><ul><li><p><strong>1. 字面量方式</strong><code>var arr = [1,&quot;test&quot;,true];</code></p></li><li><p><strong>2. 实例化数组对象 new Array()</strong><code>var arr = new Array();</code></p></li><li><ul><li>注意：上面代码中arr创建出的是一个空数组，如果需要使用构造函数Array创建非空数组，可以在创建数组时传入参数</li><li>如果只传入一个参数(数字)，则参数规定了数组的长度。</li><li>如果传入了多个参数，则参数称为数组的元素。</li></ul></li></ul><p><strong>「检测是否为数组」</strong></p><ul><li><p><strong>1. instanceof 运算符</strong></p></li><li><ul><li>instanceof 可以判断一个对象是否是某个构造函数的实例</li></ul></li></ul><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">var arr = [1, 23];</span><br><span class="line">var obj = &#123;&#125;;</span><br><span class="line">console.log(arr instanceof Array); // true</span><br><span class="line">console.log(obj instanceof Array); // false</span><br></pre></td></tr></table></figure><ul><li><p><strong>2. Array.isArray()</strong></p></li><li><ul><li>Array.isArray()用于判断一个对象是否为数组，isArray() 是 HTML5 中提供的方法</li></ul></li></ul><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">var arr = [1, 23];</span><br><span class="line">var obj = &#123;&#125;;</span><br><span class="line">console.log(Array.isArray(arr));   // true</span><br><span class="line">console.log(Array.isArray(obj));   // false</span><br></pre></td></tr></table></figure><ul><li><p><strong>3. 注意 typeof用法</strong></p></li><li><ul><li>typeof 用于判断变量的类型</li></ul></li></ul><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">var arr = [1, 23];</span><br><span class="line">console.log(typeof arr) // object 对象arr是构造函数的实例因此是对象数据类型</span><br></pre></td></tr></table></figure><p><strong>「添加删除数组元素的方法」</strong></p><ul><li>数组中有进行增加、删除元素的方法，部分方法如下表👇</li></ul><p><img src="" data-lazy-src="https://cdn1.tianli0.top/gh/web-xc/iMG/posts/xuexijs%20(7).png" alt="图片"></p><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">var arr = [1, 2, 3];</span><br><span class="line">console.log(arr.push(4, 5)); // 5 向数组末尾添加元素</span><br><span class="line">arr.pop(); //删除数组最后一个值并返回</span><br><span class="line">console.log(arr); // [1,2,3,4]</span><br><span class="line"></span><br><span class="line">// 向数组的开头添加元素并返回数组长度 </span><br><span class="line">console.log(arr.unshift(5, 6)); // 6  数组变为[5,6,1,2,3,4]</span><br><span class="line">console.log(arr.shift()); // 5 删除数组开头的元素并返回该值</span><br></pre></td></tr></table></figure><p><strong>「数组排序」</strong></p><ul><li>数组中有对数组本身排序的方法，部分方法如下表</li></ul><table><thead><tr><th align="left">方法名</th><th align="left">说明</th><th align="left">是否修改原数组</th></tr></thead><tbody><tr><td align="left">reverse()</td><td align="left">颠倒数组中元素的顺序，无参数</td><td align="left">该方法会改变原来的数组 返回新数组</td></tr><tr><td align="left">sort()</td><td align="left">对数组的元素进行排序</td><td align="left">该方法会改变原来的数组 返回新数组</td></tr></tbody></table><ul><li><p>注意：<strong>sort方法需要传入参数(函数)来设置升序、降序排序</strong></p></li><li><ul><li>如果传入“function(a,b){ return a-b;}”，则为升序</li><li>如果传入“function(a,b){ return b-a;}”，则为降序</li></ul></li></ul><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">// 踩坑 数组sort()排序(冒泡排序) return a - b 则升序</span><br><span class="line">// 写法固定 参考如下</span><br><span class="line">var arr1 = [13,4,77,1,7];</span><br><span class="line">arr1.sort(function(a,b)&#123;</span><br><span class="line">  return a-b;</span><br><span class="line">&#125;);</span><br><span class="line">console.log(arr1);</span><br></pre></td></tr></table></figure><p><strong>「数组索引方法」</strong></p><ul><li>数组中有获取数组指定元素索引值的方法，部分方法如下表</li></ul><p><img src="" data-lazy-src="" alt="图片"></p><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">var arr = [1, 2, 3, 4, 5, 4, 1, 2];</span><br><span class="line">// 查找元素2的索引</span><br><span class="line">console.log(arr.indexOf(2)); // 1</span><br><span class="line">// 查找元素1在数组中的最后一个索引</span><br><span class="line">console.log(arr.lastIndexOf(1)); // 6</span><br></pre></td></tr></table></figure><p><strong>「数组转换为字符串」</strong></p><ul><li>数组中有把数组转化为字符串的方法，部分方法如下表</li><li>注意：join方法如果不传入参数，则按照 “ , ”拼接元素<img src="" data-lazy-src="" alt="图片"></li></ul><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">var arr = [1, 2, 3, 4];</span><br><span class="line">var arr2 = arr;</span><br><span class="line">var str = arr.toString(); // 将数组转换为字符串</span><br><span class="line">console.log(str); // 1,2,3,4</span><br><span class="line">  </span><br><span class="line">var str2 = arr2.join(&quot;|&quot;);//按照键入字符将数组转换为字符串</span><br><span class="line">console.log(str2);</span><br></pre></td></tr></table></figure><p><strong>「其他方法」</strong><img src="" data-lazy-src="https://cdn1.tianli0.top/gh/web-xc/iMG/posts/xuexijs%20(6).png" alt="图片"></p><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">var arr = [1, 2, 3, 4];</span><br><span class="line">var arr2 = [5, 6, 7, 8];</span><br><span class="line">var arr3 = arr.concat(arr2);</span><br><span class="line">console.log(arr3); // [1,2,3,4,5,6,7,8]</span><br><span class="line"></span><br><span class="line">// slice(begin,end) 是一种左闭右开区间 [1,3)</span><br><span class="line">// 从索引1出开始截取，到索引3之前</span><br><span class="line">var arr4 = arr.slice(1, 3);</span><br><span class="line">console.log(arr4); // [2,3]</span><br><span class="line"></span><br><span class="line">var arr5 = arr2.splice(0, 3);</span><br><span class="line">console.log(arr5); // [5,6,7]</span><br><span class="line">console.log(arr2); // [8]   splice()会影响原数组</span><br></pre></td></tr></table></figure><h3 id="字符串对象"><a href="#字符串对象" class="headerlink" title="字符串对象"></a>字符串对象</h3><p><strong>「基本包装类型」</strong>为了方便操作基本数据类型，JavaScript 还提供了三个特殊的引用类型：String、Number和 Boolean。<br><code>基本包装类型就是把简单数据类型包装成为复杂数据类型</code>，这样基本数据类型就有了属性和方法。</p><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">// 下面代码有什么问题？</span><br><span class="line">var str = &#x27;andy&#x27;;</span><br><span class="line">console.log(str.length); // 4</span><br></pre></td></tr></table></figure><p>按道理基本数据类型是没有属性和方法的，而对象才有属性和方法，但上面代码却可以执行，这是因为 js 会把基本数据类型包装为复杂数据类型，其执行过程如下 ：</p><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">// 1. 生成临时变量，把简单类型包装为复杂数据类型</span><br><span class="line">var temp = new String(&#x27;andy&#x27;);</span><br><span class="line">// 2. 赋值给我们声明的字符变量</span><br><span class="line">str = temp;</span><br><span class="line">// 3. 销毁临时变量</span><br><span class="line">temp = null;</span><br></pre></td></tr></table></figure><p><strong>「字符串的不可变」</strong></p><ul><li>指的是里面的值不可变，虽然看上去可以改变内容，但其实是地址变了，内存中新开辟了一个内存空间。</li><li>当重新给字符串变量赋值的时候，变量之前保存的字符串不会被修改，依然在内存中重新给字符串赋值，会重新在内存中开辟空间，这个特点就是字符串的不可变。</li><li>由于字符串的不可变，在<strong>「大量拼接字符串」</strong>的时候会有效率问题</li></ul><p><strong>「根据字符返回位置」</strong></p><ul><li>字符串通过基本包装类型可以调用部分方法来操作字符串，以下是返回指定字符的位置的方法：<img src="" data-lazy-src="" alt="图片"></li></ul><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">var str = &quot;anndy&quot;;</span><br><span class="line">console.log(str.indexOf(&quot;d&quot;)); // 3</span><br><span class="line">//指定从索引号为4的地方开始查找字符&quot;d&quot;</span><br><span class="line">console.log(str.indexOf(&quot;d&quot;, 4)); // -1</span><br><span class="line"></span><br><span class="line">console.log(str.lastIndexOf(&quot;n&quot;)); // 2</span><br></pre></td></tr></table></figure><p>案例：查找字符串”abcoefoxyozzopp”中所有o出现的位置以及次数</p><ol><li>先查找第一个o出现的位置</li><li>然后 只要indexOf 返回的结果不是 -1 就继续往后查找</li><li>因为indexOf 只能查找到第一个，所以后面的查找，利用第二个参数，当前索引加1，从而继续查找</li></ol><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">var str = &quot;oabcoefoxyozzopp&quot;;</span><br><span class="line">var index = str.indexOf(&quot;o&quot;);</span><br><span class="line">var num = 0;</span><br><span class="line">while (index !== -1) &#123;</span><br><span class="line">  console.log(index);</span><br><span class="line">  num++;</span><br><span class="line">  index = str.indexOf(&quot;o&quot;, index + 1);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p><strong>「根据位置返回字符」</strong></p><ul><li>字符串通过基本包装类型可以调用部分方法来操作字符串，以下是根据位置返回指定位置上的字符：<img src="" data-lazy-src="https://cdn1.tianli0.top/gh/web-xc/iMG/posts/xuexijs%20(5).png" alt="图片"></li></ul><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">// 根据位置返回字符</span><br><span class="line">// 1. charAt(index) 根据位置返回字符</span><br><span class="line">var str = &#x27;andy&#x27;;</span><br><span class="line">console.log(str.charAt(3)); // y</span><br><span class="line">// 遍历所有的字符</span><br><span class="line">for (var i = 0; i &lt; str.length; i++) &#123;</span><br><span class="line">    console.log(str.charAt(i));</span><br><span class="line">&#125; // a n d y</span><br><span class="line"></span><br><span class="line">// 2. charCodeAt(index)  </span><br><span class="line">//返回相应索引号的字符ASCII值 目的： 判断用户按下了那个键 </span><br><span class="line">console.log(str.charCodeAt(0)); // 97</span><br><span class="line">// 3. str[index] H5 新增的</span><br><span class="line">console.log(str[0]); // a</span><br></pre></td></tr></table></figure><ul><li><p>案例：判断一个字符串 ‘abcoefoxyozzopp’ 中出现次数最多的字符，并统计其次数</p></li><li><ol><li>核心算法：利用 charAt(） 遍历这个字符串</li><li>把每个字符都存储给对象， 如果对象没有该属性，就为1，如果存在了就 +1</li><li>遍历对象，得到最大值和该字符 注意：在遍历的过程中，把字符串中的每个字符作为对象的属性存储在对象中，对应的属性值是该字符出现的次数</li></ol></li></ul><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line">var str = &quot;abcoefoxyozzopp&quot;;</span><br><span class="line">var o = &#123;&#125;;</span><br><span class="line">for (var i = 0; i &lt; str.length; i++) &#123;</span><br><span class="line">  var chars = str.charAt(i); // chars 是 字符串的每一个字符</span><br><span class="line">  if (o[chars]) &#123;</span><br><span class="line">    // o[chars] 得到的是属性值</span><br><span class="line">    o[chars]++;</span><br><span class="line">  &#125; else &#123;</span><br><span class="line">    o[chars] = 1;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line">console.log(o);</span><br><span class="line">// 2. 遍历对象</span><br><span class="line">var max = 0;</span><br><span class="line">var ch = &quot;&quot;;</span><br><span class="line">for (var k in o) &#123;</span><br><span class="line">  // k 得到是 属性名</span><br><span class="line">  // o[k] 得到的是属性值</span><br><span class="line">  if (o[k] &gt; max) &#123;</span><br><span class="line">    max = o[k];</span><br><span class="line">    ch = k;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line">console.log(max);</span><br><span class="line">console.log(&quot;最多的字符是&quot; + ch);</span><br></pre></td></tr></table></figure><p><strong>「字符串操作方法」</strong></p><ul><li>字符串通过基本包装类型可以调用部分方法来操作字符串，以下是部分操作方法：<img src="" data-lazy-src="https://cdn1.tianli0.top/gh/web-xc/iMG/posts/xuexijs%20(4).png" alt="图片"></li></ul><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">// 字符串操作方法</span><br><span class="line">// 1. concat(&#x27;字符串1&#x27;,&#x27;字符串2&#x27;....)</span><br><span class="line">var str = &#x27;andy&#x27;;</span><br><span class="line">console.log(str.concat(&#x27;red&#x27;)); // andyred</span><br><span class="line"></span><br><span class="line">// 2. substr(&#x27;截取的起始位置&#x27;, &#x27;截取几个字符&#x27;);</span><br><span class="line">var str1 = &#x27;改革春风吹满地&#x27;;</span><br><span class="line">// 第一个2 是索引号的2 从第几个开始  第二个2 是取几个字符</span><br><span class="line">console.log(str1.substr(2, 2)); // 春风</span><br></pre></td></tr></table></figure><ul><li><p><strong>replace()方法</strong></p></li><li><ul><li>replace() 方法用于在字符串中用一些字符替换另一些字符，其使用格式如下：</li></ul></li></ul><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">字符串.replace(被替换的字符串， 要替换为的字符串)；</span><br></pre></td></tr></table></figure><ul><li><p><strong>split()方法</strong></p></li><li><ul><li>split()方法用于切分字符串，它可以将字符串切分为数组。在切分完毕之后，返回的是一个新数组。</li><li>其使用格式如下：</li></ul></li></ul><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">字符串.split(&quot;分割字符&quot;)</span><br><span class="line">    // 1. 替换字符 replace(&#x27;被替换的字符&#x27;, &#x27;替换为的字符&#x27;)  它只会替换第一个字符</span><br><span class="line">    var str = &quot;andyandy&quot;;</span><br><span class="line">    console.log(str.replace(&quot;a&quot;, &quot;b&quot;)); // bndyandy</span><br><span class="line">    // 有一个字符串 &#x27;abcoefoxyozzopp&#x27;  要求把里面所有的 o 替换为 *</span><br><span class="line">    var str1 = &quot;abcoefoxyozzopp&quot;;</span><br><span class="line">    while (str1.indexOf(&quot;o&quot;) !== -1) &#123;</span><br><span class="line">      str1 = str1.replace(&quot;o&quot;, &quot;*&quot;);</span><br><span class="line">    &#125;</span><br><span class="line">    console.log(str1); // abc*ef*xy*zz*pp</span><br><span class="line"></span><br><span class="line">    // 2. 字符转换为数组 split(&#x27;分隔符&#x27;)    </span><br><span class="line">    // 前面我们学过 join 把数组转换为字符串</span><br><span class="line">    var str2 = &quot;red, pink, blue&quot;;</span><br><span class="line">    console.log(str2.split(&quot;,&quot;)); //[red,pink,blue]</span><br><span class="line">    var str3 = &quot;red&amp;pink&amp;blue&quot;;</span><br><span class="line">    console.log(str3.split(&quot;&amp;&quot;)); // [red,pink,blue]</span><br></pre></td></tr></table></figure><hr><h2 id="简单数据类型和复杂数据类型"><a href="#简单数据类型和复杂数据类型" class="headerlink" title="简单数据类型和复杂数据类型"></a>简单数据类型和复杂数据类型</h2><p><strong>「简单类型（基本数据类型、值类型）」</strong>：在存储时变量中存储的是值本身，包括string ，number，boolean，undefined，null</p><p><strong>「复杂数据类型（引用类型）」</strong>：在存储时变量中存储的仅仅是地址（引用），通过 new 关键字创建的对象（系统对象、自定义对象），如 Object、Array、Date等；</p><p><strong>「堆栈」</strong></p><ul><li><p>堆栈空间分配区别：</p></li><li><ul><li>1、栈（操作系统）：由操作系统自动分配释放存放函数的参数值、局部变量的值等。其操作方式类似于数据结构中的栈；</li><li>简单数据类型存放到栈里面</li><li>2、堆（操作系统）：存储复杂类型(对象)，一般由程序员分配释放，若程序员不释放，由垃圾回收机制回收。</li></ul></li></ul><p><img src="" data-lazy-src="" alt="图片"></p><ul><li><p>简单数据类型的存储方式</p></li><li><ul><li>值类型变量的数据直接存放在变量（栈空间）中<img src="" data-lazy-src="https://cdn1.tianli0.top/gh/web-xc/iMG/posts/xuexijs%20(3).png" alt="图片"></li></ul></li><li><p>复杂数据类型的存储方式</p></li><li><ul><li>引用类型变量（栈空间）里存放的是地址，真正的对象实例存放在堆空间中<img src="" data-lazy-src="https://cdn1.tianli0.top/gh/web-xc/iMG/posts/xuexijs%20(2).png" alt="图片"></li></ul></li></ul><p><strong>「简单类型传参」</strong><br>函数的形参也可以看做是一个变量，当我们把一个值类型变量作为参数传给函数的形参时，其实是把变量在栈空间里的值复制了一份给形参，那么在方法内部对形参做任何修改，都不会影响到的外部变量。</p><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">function fn(a) &#123;</span><br><span class="line">    a++;</span><br><span class="line">    console.log(a); </span><br><span class="line">&#125;</span><br><span class="line">var x = 10;</span><br><span class="line">fn(x);</span><br><span class="line">console.log(x)；</span><br></pre></td></tr></table></figure><p>运行结果如下<img src="" data-lazy-src="https://cdn1.tianli0.top/gh/web-xc/iMG/posts/xuexijs%20(1).png" alt="图片"></p><p><strong>「复杂数据类型传参」</strong><br>函数的形参也可以看做是一个变量，当我们把引用类型变量传给形参时，其实是把变量在栈空间里保存的堆地址复制给了形参，形参和实参其实保存的是同一个堆地址，所以操作的是同一个对象。</p><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">function Person(name) &#123;</span><br><span class="line">    this.name = name;</span><br><span class="line">&#125;</span><br><span class="line">function f1(x) &#123; // x = p</span><br><span class="line">    console.log(x.name); // 2. 这个输出什么 ?    </span><br><span class="line">    x.name = &quot;张学友&quot;;</span><br><span class="line">    console.log(x.name); // 3. 这个输出什么 ?    </span><br><span class="line">&#125;</span><br><span class="line">var p = new Person(&quot;刘德华&quot;);</span><br><span class="line">console.log(p.name);    // 1. 这个输出什么 ?   </span><br><span class="line">f1(p);</span><br><span class="line">console.log(p.name);    // 4. 这个输出什么 ?  </span><br></pre></td></tr></table></figure><p>运行结果如下：<img src="" data-lazy-src="https://cdn1.tianli0.top/gh/web-xc/iMG/posts/xuexijs%20(11).png" alt="图片"></p></article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">本文作者:</span> <span class="post-copyright-info"><a href="https://www.webxc.ml">XC's Blog</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">本文链接:</span> <span class="post-copyright-info"><a href="https://www.webxc.ml/post/2847555892/">https://www.webxc.ml/post/2847555892/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明:</span> <span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="https://www.webxc.ml" target="_blank">XC's Blog</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/%E5%89%8D%E7%AB%AF%E7%AC%94%E8%AE%B0/">前端笔记</a></div><div class="post_share"><div class="social-share" data-image="https://npm.elemecdn.com/webxc@1.0.0/cover/js1.jpg" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://npm.elemecdn.com/butterfly-extsrc/sharejs/dist/css/share.min.css" media="print" onload='this.media="all"'><script src="https://npm.elemecdn.com/butterfly-extsrc/sharejs/dist/js/social-share.min.js" defer></script></div></div><div class="post-reward"><div class="reward-button"><i class="fas fa-qrcode"></i> 打赏</div><div class="reward-main"><ul class="reward-all"><li class="reward-item"><a href="https://npm.elemecdn.com/webxc@1.0.0/lxfs/wechat.png" target="_blank"><img class="post-qr-code-img" src="https://npm.elemecdn.com/webxc@1.0.0/lxfs/wechat.png" alt="Wechat"></a><div class="post-qr-code-desc">Wechat</div></li><li class="reward-item"><a href="https://npm.elemecdn.com/webxc@1.0.0/lxfs/alipay.jpg" target="_blank"><img class="post-qr-code-img" src="https://npm.elemecdn.com/webxc@1.0.0/lxfs/alipay.jpg" alt="Alipay"></a><div class="post-qr-code-desc">Alipay</div></li></ul></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/post/598947694/"><img class="prev-cover" src="https://npm.elemecdn.com/webxc@1.0.0/cover/html2.jpg" onerror='onerror=null,src="https://npm.elemecdn.com/webxc@1.0.2/banner/404.gif"' alt="cover of previous post"><div class="pagination-info"><div class="label">上一篇文章</div><div class="prev_info">HTML与CSS进阶</div></div></a></div><div class="next-post pull-right"><a href="/post/3876629914/"><img class="next-cover" src="https://npm.elemecdn.com/webxc@1.0.0/cover/cover7.jpg" onerror='onerror=null,src="https://npm.elemecdn.com/webxc@1.0.2/banner/404.gif"' alt="cover of next post"><div class="pagination-info"><div class="label">下一篇文章</div><div class="next_info">HTML第一天</div></div></a></div></nav><div class="relatedPosts"><div class="headline"><i class="fas fa-thumbs-up fa-fw"></i><span>相关推荐</span></div><div class="relatedPosts-list"><div><a href="/post/60656315/" title="「学习笔记」HTML基础"><img class="cover" src="https://npm.elemecdn.com/webxc@1.0.0/cover/cover7.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-01-01</div><div class="title">「学习笔记」HTML基础</div></div></a></div><div><a href="/post/264246537/" title="「学习笔记」CSS基础"><img class="cover" src="https://npm.elemecdn.com/webxc@1.0.0/cover/css1.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-01-02</div><div class="title">「学习笔记」CSS基础</div></div></a></div></div></div><hr><div id="post-comment"><div class="comment-head"><div class="comment-headline"><i class="fas fa-comments fa-fw"></i> <span>评论</span></div></div><div class="comment-wrap"><div><div id="twikoo-wrap"></div></div></div></div></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src="https://npm.elemecdn.com/webxc@1.0.0/logo/logo.jpg" onerror='this.onerror=null,this.src="https://npm.elemecdn.com/webxc@1.0.2/banner/404.gif"' alt="avatar"></div><div class="author-info__name">XC's Blog</div><div class="author-info__description">XC's Blog 记录生活和学习文章</div></div><div class="card-info-data site-data is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">53</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">18</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">18</div></a></div><div class="card-info-social-icons is-center"><a class="social-icon" href="https://tenapi.cn/qqzlk/?qq=2848141597" target="_blank" title="QQ"><i class="iconfont icon-QQ"></i></a><a class="social-icon" href="https://npm.elemecdn.com/webxc@1.0.0/lxfs/wechat.jpg" target="_blank" title="微信"><i class="iconfont icon-weixin"></i></a><a class="social-icon" href="https://github.com/web-xc" target="_blank" title="Github"><i class="iconfont icon-github"></i></a><a class="social-icon" href="https://space.bilibili.com/2134826455" target="_blank" title="BiliBili"><i class="iconfont icon-bilibili"></i></a><a class="social-icon" href="https://music.163.com/#/user/home?id=323682437" target="_blank" title="网易云音乐"><i class="iconfont icon-wyy"></i></a></div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span><span class="toc-percentage"></span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E3%80%8C%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0%E3%80%8DJavaScript%E5%9F%BA%E7%A1%80"><span class="toc-number">1.</span> <span class="toc-text">「学习笔记」JavaScript基础</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E7%BC%96%E7%A8%8B%E5%9F%BA%E7%A1%80"><span class="toc-number">1.1.</span> <span class="toc-text">编程基础</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%88%9D%E8%AF%86JavaScript"><span class="toc-number">2.</span> <span class="toc-text">初识JavaScript</span></a></li></ol><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%8F%98%E9%87%8F"><span class="toc-number"></span> <span class="toc-text">变量</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%8F%98%E9%87%8F%E7%9A%84%E6%A6%82%E5%BF%B5"><span class="toc-number">1.</span> <span class="toc-text">变量的概念</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%8F%98%E9%87%8F%E7%9A%84%E4%BD%BF%E7%94%A8"><span class="toc-number">2.</span> <span class="toc-text">变量的使用</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%95%B0%E6%8D%AE%E7%B1%BB%E5%9E%8B"><span class="toc-number"></span> <span class="toc-text">数据类型</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#Number%E6%95%B0%E5%AD%97%E5%9E%8B"><span class="toc-number">1.</span> <span class="toc-text">Number数字型</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#String%E5%AD%97%E7%AC%A6%E4%B8%B2%E5%9E%8B"><span class="toc-number">2.</span> <span class="toc-text">String字符串型</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%B8%83%E5%B0%94%E5%9E%8BBoolean"><span class="toc-number">3.</span> <span class="toc-text">布尔型Boolean</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#Undefined-%E5%92%8C-Null"><span class="toc-number">4.</span> <span class="toc-text">Undefined 和 Null</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E8%8E%B7%E5%8F%96%E5%8F%98%E9%87%8F%E7%B1%BB%E5%9E%8B%E5%8F%8A%E8%BD%AC%E6%8D%A2"><span class="toc-number">5.</span> <span class="toc-text">获取变量类型及转换</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%85%B3%E9%94%AE%E5%AD%97%E5%92%8C%E4%BF%9D%E7%95%99%E5%AD%97"><span class="toc-number">6.</span> <span class="toc-text">关键字和保留字</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E8%BF%90%E7%AE%97%E7%AC%A6%E4%B8%8E%E6%B5%81%E7%A8%8B%E6%8E%A7%E5%88%B6"><span class="toc-number">7.</span> <span class="toc-text">运算符与流程控制</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E8%BF%90%E7%AE%97%E7%AC%A6-%E6%93%8D%E4%BD%9C%E7%AC%A6"><span class="toc-number">7.1.</span> <span class="toc-text">运算符(操作符)</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%B5%81%E7%A8%8B%E6%8E%A7%E5%88%B6"><span class="toc-number">8.</span> <span class="toc-text">流程控制</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%BE%AA%E7%8E%AF%E4%B8%8E%E4%BB%A3%E7%A0%81%E8%A7%84%E8%8C%83"><span class="toc-number">9.</span> <span class="toc-text">循环与代码规范</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%BE%AA%E7%8E%AF"><span class="toc-number">9.1.</span> <span class="toc-text">循环</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%BB%A3%E7%A0%81%E8%A7%84%E8%8C%83"><span class="toc-number">10.</span> <span class="toc-text">代码规范</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%95%B0%E7%BB%84%E4%B8%8E%E5%87%BD%E6%95%B0"><span class="toc-number"></span> <span class="toc-text">数组与函数</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%95%B0%E7%BB%84"><span class="toc-number">1.</span> <span class="toc-text">数组</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%87%BD%E6%95%B0"><span class="toc-number">2.</span> <span class="toc-text">函数</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%BD%9C%E7%94%A8%E5%9F%9F"><span class="toc-number"></span> <span class="toc-text">作用域</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%BD%9C%E7%94%A8%E5%9F%9F-1"><span class="toc-number">1.</span> <span class="toc-text">作用域</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%8F%98%E9%87%8F%E7%9A%84%E4%BD%9C%E7%94%A8%E5%9F%9F"><span class="toc-number">2.</span> <span class="toc-text">变量的作用域</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%BD%9C%E7%94%A8%E5%9F%9F%E9%93%BE"><span class="toc-number">3.</span> <span class="toc-text">作用域链</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E9%A2%84%E8%A7%A3%E6%9E%90"><span class="toc-number">4.</span> <span class="toc-text">预解析</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%AF%B9%E8%B1%A1"><span class="toc-number">5.</span> <span class="toc-text">对象</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%AF%B9%E8%B1%A1%E7%9A%84%E6%A6%82%E5%BF%B5"><span class="toc-number">5.1.</span> <span class="toc-text">对象的概念</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%88%9B%E5%BB%BA%E5%AF%B9%E8%B1%A1%E7%9A%84%E4%B8%89%E7%A7%8D%E6%96%B9%E5%BC%8F"><span class="toc-number">5.2.</span> <span class="toc-text">创建对象的三种方式</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E9%81%8D%E5%8E%86%E5%AF%B9%E8%B1%A1"><span class="toc-number">6.</span> <span class="toc-text">遍历对象</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%86%85%E7%BD%AE%E5%AF%B9%E8%B1%A1"><span class="toc-number"></span> <span class="toc-text">内置对象</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#Math%E5%AF%B9%E8%B1%A1"><span class="toc-number">1.</span> <span class="toc-text">Math对象</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%97%A5%E6%9C%9F%E5%AF%B9%E8%B1%A1"><span class="toc-number">2.</span> <span class="toc-text">日期对象</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%95%B0%E7%BB%84%E5%AF%B9%E8%B1%A1"><span class="toc-number">3.</span> <span class="toc-text">数组对象</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%AD%97%E7%AC%A6%E4%B8%B2%E5%AF%B9%E8%B1%A1"><span class="toc-number">4.</span> <span class="toc-text">字符串对象</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E7%AE%80%E5%8D%95%E6%95%B0%E6%8D%AE%E7%B1%BB%E5%9E%8B%E5%92%8C%E5%A4%8D%E6%9D%82%E6%95%B0%E6%8D%AE%E7%B1%BB%E5%9E%8B"><span class="toc-number"></span> <span class="toc-text">简单数据类型和复杂数据类型</span></a></li></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/post/f4e7/" title="3D转换-转后不显示问题"><img src="https://npm.elemecdn.com/webxc@1.0.11/cover/css5.jpg" onerror='this.onerror=null,this.src="https://npm.elemecdn.com/webxc@1.0.2/banner/404.gif"' alt="3D转换-转后不显示问题"></a><div class="content"><a class="title" href="/post/f4e7/" title="3D转换-转后不显示问题">3D转换-转后不显示问题</a><time datetime="2022-10-20T16:06:00.000Z" title="发表于 2022-10-20 16:06:00">2022-10-20</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/post/d97/" title="CSS-2D-3D转换"><img src="https://npm.elemecdn.com/webxc@1.0.11/cover/css5.jpg" onerror='this.onerror=null,this.src="https://npm.elemecdn.com/webxc@1.0.2/banner/404.gif"' alt="CSS-2D-3D转换"></a><div class="content"><a class="title" href="/post/d97/" title="CSS-2D-3D转换">CSS-2D-3D转换</a><time datetime="2022-10-16T23:54:00.000Z" title="发表于 2022-10-16 23:54:00">2022-10-16</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/post/bea0/" title="前端168个单词"><img src="https://npm.elemecdn.com/webxc@1.0.11/cover/css6.jpg" onerror='this.onerror=null,this.src="https://npm.elemecdn.com/webxc@1.0.2/banner/404.gif"' alt="前端168个单词"></a><div class="content"><a class="title" href="/post/bea0/" title="前端168个单词">前端168个单词</a><time datetime="2022-10-15T18:00:00.000Z" title="发表于 2022-10-15 18:00:00">2022-10-15</time></div></div></div></div></div></div></main><footer id="footer"><div id="footer-wrap"><div class="copyright">&copy;2021 - 2022 By XC's Blog</div><div class="framework-info"><span></span> <a target="_blank" rel="noopener" href="https://icp.gov.moe/?keyword=20221916">萌ICP备20221916号</a><span class="footer-separator">|</span><span></span> <a target="_blank" rel="noopener" href="https://icp.gov.moe/">萌ICP查询</a><div id="running-time"></div></div><script>setInterval((()=>{let e=Math.round(new Date("2020-11-21 01:35:06").getTime()/1e3),t=Math.round(((new Date).getTime()+288e5)/1e3)-e,n=new Array(0,0,0,0,0);t>=31536e3&&(n[0]=parseInt(t/31536e3),t%=31536e3),t>=86400&&(n[1]=parseInt(t/86400),t%=86400),t>=3600&&(n[2]=parseInt(t/3600),t%=3600),t>=60&&(n[3]=parseInt(t/60),t%=60),t>0&&(n[4]=t),currentTimeHtml="本站已安全运行 "+n[0]+" 年 "+n[1]+" 天 "+n[2]+" 时 "+n[3]+" 分 "+n[4]+" 秒",document.getElementById("running-time").innerHTML=currentTimeHtml}),1e3)</script><div class="footer_custom_text"><p><a style="margin-inline:5px" target="_blank" href="https://hexo.io/"><img src="https://img.shields.io/badge/Frame-Hexo-blue?style=flat&logo=hexo" title="博客框架为 Hexo" alt="HEXO"></a><a style="margin-inline:5px" target="_blank" href="https://www.jsdelivr.com/"><img src="https://img.shields.io/badge/CDN-jsDelivr-orange?style=flat&logo=jsDelivr" title="本站使用 Jsdelivr 为静态资源提供CDN加速" alt="Jsdelivr"></a><a style="margin-inline:5px" target="_blank" href="https://github.com/web-xc"><img src="https://img.shields.io/badge/Source-Github-d021d6?style=flat&logo=GitHub" title="本站项目由 GitHub 托管" alt="GitHub"></a></p></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><a id="to_comment" href="#post-comment" title="直达评论"><i class="fas fa-comments"></i></a><button id="go-up" type="button" title="回到顶部"><i class="fas fa-arrow-up"></i></button></div></div><div id="local-search"><div class="search-dialog"><nav class="search-nav"><span class="search-dialog-title">搜索</span><span id="loading-status"></span><button class="search-close-button"><i class="fas fa-times"></i></button></nav><div class="is-center" id="loading-database"><i class="fas fa-spinner fa-pulse"></i> <span>正在载入文件,请稍后</span></div><div class="search-wrap"><div id="local-search-input"><div class="local-search-box"><input class="local-search-box--input" placeholder="搜索文章" type="text"></div></div><hr><div id="local-search-results"></div></div></div><div id="search-mask"></div></div><div><script src="https://npm.elemecdn.com/hexo-theme-butterfly/source/js/utils.js"></script><script src="https://cdn.bootcdn.net/ajax/libs/hexo-theme-butterfly/4.4.0/js/main.min.js"></script><script src="https://npm.elemecdn.com/@fancyapps/ui/dist/fancybox.umd.js"></script><script src="https://npm.elemecdn.com/instant.page/instantpage.js" type="module"></script><script src="https://npm.elemecdn.com/vanilla-lazyload/dist/lazyload.iife.min.js"></script><script src="https://npm.elemecdn.com/node-snackbar/dist/snackbar.min.js"></script><script src="https://cdn.bootcdn.net/ajax/libs/hexo-theme-butterfly/4.4.0/js/search/local-search.min.js"></script><div class="js-pjax"><script>(()=>{const e=document.querySelectorAll("#article-container .mermaid-wrap");if(e.length){window.runMermaid=()=>{window.loadMermaid=!0;const t="dark"===document.documentElement.getAttribute("data-theme")?"dark":"default";Array.from(e).forEach(((e,n)=>{const d=e.firstElementChild,r="mermaid-"+n,m="%%{init:{ 'theme':'"+t+"'}}%%\n"+d.textContent;mermaid.mermaidAPI.render(r,m,(e=>{d.insertAdjacentHTML("afterend",e)}))}))};const t=()=>{window.loadMermaid?runMermaid():getScript("https://npm.elemecdn.com/mermaid/dist/mermaid.min.js").then(runMermaid)};window.pjax?t():document.addEventListener("DOMContentLoaded",t)}})()</script><script>(()=>{const o=()=>{twikoo.init(Object.assign({el:"#twikoo-wrap",envId:"https://love1.webxc.ml/",region:"",onCommentLoaded:function(){btf.loadLightbox(document.querySelectorAll("#twikoo .tk-content img:not(.tk-owo-emotion)"))}},null))},t=()=>{"object"!=typeof twikoo?getScript("https://cdn.bootcdn.net/ajax/libs/twikoo/1.6.7/twikoo.all.min.js").then(o):setTimeout(o,0)};t()})()</script></div><script src="https://npm.elemecdn.com/webxc@1.0.6/css/style.js"></script><script data-pjax src="https://npm.elemecdn.com/pace-js@1.2.4/pace.min.js"></script><script src="https://npm.elemecdn.com/butterfly-extsrc/dist/activate-power-mode.min.js"></script><script>POWERMODE.colorful=!0,POWERMODE.shake=!0,POWERMODE.mobile=!0,document.body.addEventListener("input",POWERMODE)</script><script src="https://npm.elemecdn.com/pjax/pjax.min.js"></script><script>let pjaxSelectors=["head > title","#config-diff","#body-wrap","#rightside-config-hide","#rightside-config-show",".js-pjax",".tk-comments-container"];var pjax=new Pjax({elements:'a:not([target="_blank"])',selectors:pjaxSelectors,cacheBust:!1,analytics:!1,scrollRestoration:!1});document.addEventListener("pjax:send",(function(){if(window.tocScrollFn&&window.removeEventListener("scroll",window.tocScrollFn),window.scrollCollect&&window.removeEventListener("scroll",scrollCollect),"object"==typeof preloader&&preloader.initLoading(),document.getElementById("rightside").style.cssText="opacity: ''; transform: ''",window.aplayers)for(let e=0;e<window.aplayers.length;e++)window.aplayers[e].options.fixed||window.aplayers[e].destroy();"object"==typeof typed&&typed.destroy();const e=document.body.classList;e.contains("read-mode")&&e.remove("read-mode"),"object"==typeof disqusjs&&disqusjs.destroy()})),document.addEventListener("pjax:complete",(function(){window.refreshFn(),document.querySelectorAll("script[data-pjax]").forEach((e=>{const t=document.createElement("script"),o=e.text||e.textContent||e.innerHTML||"";Array.from(e.attributes).forEach((e=>t.setAttribute(e.name,e.value))),t.appendChild(document.createTextNode(o)),e.parentNode.replaceChild(t,e)})),GLOBAL_CONFIG.islazyload&&window.lazyLoadInstance.update(),"function"==typeof chatBtnFn&&chatBtnFn(),"function"==typeof panguInit&&panguInit(),"function"==typeof gtag&&gtag("config","",{page_path:window.location.pathname}),"object"==typeof _hmt&&_hmt.push(["_trackPageview",window.location.pathname]),"function"==typeof loadMeting&&document.getElementsByClassName("aplayer").length&&loadMeting(),"object"==typeof Prism&&Prism.highlightAll(),"object"==typeof preloader&&preloader.endLoading()})),document.addEventListener("pjax:error",(e=>{404===e.request.status&&pjax.loadUrl("/404.html")}))</script><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div></body></html>